| 1 | //go:build unix |
| 2 | |
| 3 | package thin |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "context" |
| 8 | "log/slog" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "syscall" |
| 14 | "testing" |
| 15 | "time" |
| 16 | ) |
| 17 | |
| 18 | // A command whose grandchild ignores SIGTERM and holds its output open |
| 19 | // cannot hold run past its timeout: the whole process group is killed |
| 20 | // when the context ends, and WaitDelay bounds the wait for the pipes. |
| 21 | // The grandchild is gone afterwards. |
| 22 | func TestMacRunEndsAHungGrandchild(t *testing.T) { |
| 23 | pidFile := filepath.Join(t.TempDir(), "grandchild") |
| 24 | pid := 0 |
| 25 | t.Cleanup(func() { |
| 26 | // Only the grandchild this test started, by pid, if it is still |
| 27 | // that sleep. |
| 28 | if pid > 0 && grandchildAlive(pid) { |
| 29 | _ = syscall.Kill(pid, syscall.SIGKILL) |
| 30 | } |
| 31 | }) |
| 32 | const timeout = 300 * time.Millisecond |
| 33 | script := `sh -c 'trap "" TERM; echo $$ > "$1"; exec sleep 15' grandchild "$1" & wait` |
| 34 | ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 35 | defer cancel() |
| 36 | start := time.Now() |
| 37 | _, err := (&macHost{}).run(ctx, "/bin/sh", "-c", script, "child", pidFile) |
| 38 | took := time.Since(start) |
| 39 | if raw, rerr := os.ReadFile(pidFile); rerr == nil { |
| 40 | pid, _ = strconv.Atoi(strings.TrimSpace(string(raw))) |
| 41 | } |
| 42 | if limit := timeout + commandWaitDelay + 2*time.Second; took > limit { |
| 43 | t.Fatalf("run took %s, past its timeout and WaitDelay (%s): err %v", took.Round(time.Millisecond), limit, err) |
| 44 | } |
| 45 | if err == nil { |
| 46 | t.Fatal("a command killed at its timeout succeeded") |
| 47 | } |
| 48 | if pid <= 0 { |
| 49 | t.Fatal("the grandchild never started") |
| 50 | } |
| 51 | for deadline := time.Now().Add(2 * time.Second); grandchildAlive(pid); time.Sleep(20 * time.Millisecond) { |
| 52 | if time.Now().After(deadline) { |
| 53 | t.Fatalf("the grandchild %d outlived run", pid) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // A command that exits at once but leaves a helper holding its output |
| 59 | // answers within WaitDelay with what it printed, and the helper, left in |
| 60 | // the command's own group, is gone. |
| 61 | func TestMacRunAnswersPastALingeringHelper(t *testing.T) { |
| 62 | was := commandWaitDelay |
| 63 | commandWaitDelay = 200 * time.Millisecond |
| 64 | defer func() { commandWaitDelay = was }() |
| 65 | pidFile := filepath.Join(t.TempDir(), "helper") |
| 66 | pid := 0 |
| 67 | t.Cleanup(func() { |
| 68 | if pid > 0 && grandchildAlive(pid) { |
| 69 | _ = syscall.Kill(pid, syscall.SIGKILL) |
| 70 | } |
| 71 | }) |
| 72 | script := `echo 1.2.3; sh -c 'echo $$ > "$1"; exec sleep 15' helper "$1" & while [ ! -s "$1" ]; do sleep 0.01; done` |
| 73 | start := time.Now() |
| 74 | var logs bytes.Buffer |
| 75 | host := &macHost{log: slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug}))} |
| 76 | out, err := host.run(context.Background(), "/bin/sh", "-c", script, "child", pidFile) |
| 77 | took := time.Since(start) |
| 78 | if raw, rerr := os.ReadFile(pidFile); rerr == nil { |
| 79 | pid, _ = strconv.Atoi(strings.TrimSpace(string(raw))) |
| 80 | } |
| 81 | if pid <= 0 { |
| 82 | t.Fatal("the helper never started") |
| 83 | } |
| 84 | if err != nil || out != "1.2.3\n" || took > 3*time.Second { |
| 85 | t.Fatalf("run answered %q %v after %s", out, err, took.Round(time.Millisecond)) |
| 86 | } |
| 87 | // The leftover is said once, at debug only: it is not a failure. |
| 88 | 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") { |
| 89 | t.Fatalf("the leftover helper was not logged at debug only: %q", l) |
| 90 | } |
| 91 | for deadline := time.Now().Add(2 * time.Second); pid > 0 && grandchildAlive(pid); time.Sleep(20 * time.Millisecond) { |
| 92 | if time.Now().After(deadline) { |
| 93 | t.Fatalf("the helper %d outlived run", pid) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // grandchildAlive: pid is a live process (not a zombie) running sleep. |
| 99 | func grandchildAlive(pid int) bool { |
| 100 | if syscall.Kill(pid, 0) != nil { |
| 101 | return false |
| 102 | } |
| 103 | if stat, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat"); err == nil { |
| 104 | if i := strings.LastIndexByte(string(stat), ')'); i > 0 && i+2 < len(stat) && stat[i+2] == 'Z' { |
| 105 | return false |
| 106 | } |
| 107 | } |
| 108 | if cmdline, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/cmdline"); err == nil { |
| 109 | return strings.Contains(string(cmdline), "sleep") |
| 110 | } |
| 111 | return true |
| 112 | } |
| 113 | |