//go:build unix package thin import ( "bytes" "context" "log/slog" "os" "path/filepath" "strconv" "strings" "syscall" "testing" "time" ) // A command whose grandchild ignores SIGTERM and holds its output open // cannot hold run past its timeout: the whole process group is killed // when the context ends, and WaitDelay bounds the wait for the pipes. // The grandchild is gone afterwards. func TestMacRunEndsAHungGrandchild(t *testing.T) { pidFile := filepath.Join(t.TempDir(), "grandchild") pid := 0 t.Cleanup(func() { // Only the grandchild this test started, by pid, if it is still // that sleep. if pid > 0 && grandchildAlive(pid) { _ = syscall.Kill(pid, syscall.SIGKILL) } }) const timeout = 300 * time.Millisecond script := `sh -c 'trap "" TERM; echo $$ > "$1"; exec sleep 15' grandchild "$1" & wait` ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() start := time.Now() _, err := (&macHost{}).run(ctx, "/bin/sh", "-c", script, "child", pidFile) took := time.Since(start) if raw, rerr := os.ReadFile(pidFile); rerr == nil { pid, _ = strconv.Atoi(strings.TrimSpace(string(raw))) } if limit := timeout + commandWaitDelay + 2*time.Second; took > limit { t.Fatalf("run took %s, past its timeout and WaitDelay (%s): err %v", took.Round(time.Millisecond), limit, err) } if err == nil { t.Fatal("a command killed at its timeout succeeded") } if pid <= 0 { t.Fatal("the grandchild never started") } for deadline := time.Now().Add(2 * time.Second); grandchildAlive(pid); time.Sleep(20 * time.Millisecond) { if time.Now().After(deadline) { t.Fatalf("the grandchild %d outlived run", pid) } } } // A command that exits at once but leaves a helper holding its output // answers within WaitDelay with what it printed, and the helper, left in // the command's own group, is gone. func TestMacRunAnswersPastALingeringHelper(t *testing.T) { was := commandWaitDelay commandWaitDelay = 200 * time.Millisecond defer func() { commandWaitDelay = was }() pidFile := filepath.Join(t.TempDir(), "helper") pid := 0 t.Cleanup(func() { if pid > 0 && grandchildAlive(pid) { _ = syscall.Kill(pid, syscall.SIGKILL) } }) script := `echo 1.2.3; sh -c 'echo $$ > "$1"; exec sleep 15' helper "$1" & while [ ! -s "$1" ]; do sleep 0.01; done` start := time.Now() var logs bytes.Buffer host := &macHost{log: slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug}))} out, err := host.run(context.Background(), "/bin/sh", "-c", script, "child", pidFile) took := time.Since(start) if raw, rerr := os.ReadFile(pidFile); rerr == nil { pid, _ = strconv.Atoi(strings.TrimSpace(string(raw))) } if pid <= 0 { t.Fatal("the helper never started") } if err != nil || out != "1.2.3\n" || took > 3*time.Second { t.Fatalf("run answered %q %v after %s", out, err, took.Round(time.Millisecond)) } // The leftover is said once, at debug only: it is not a failure. if l := logs.String(); !strings.Contains(l, "level=DEBUG msg=\"command left a helper holding its output; helper killed\"") || strings.Contains(l, "level=WARN") || strings.Contains(l, "level=ERROR") { t.Fatalf("the leftover helper was not logged at debug only: %q", l) } for deadline := time.Now().Add(2 * time.Second); pid > 0 && grandchildAlive(pid); time.Sleep(20 * time.Millisecond) { if time.Now().After(deadline) { t.Fatalf("the helper %d outlived run", pid) } } } // grandchildAlive: pid is a live process (not a zombie) running sleep. func grandchildAlive(pid int) bool { if syscall.Kill(pid, 0) != nil { return false } if stat, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat"); err == nil { if i := strings.LastIndexByte(string(stat), ')'); i > 0 && i+2 < len(stat) && stat[i+2] == 'Z' { return false } } if cmdline, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/cmdline"); err == nil { return strings.Contains(string(cmdline), "sleep") } return true }