From 5d211f7730697482da5fed8833f583a0b9d97d4f Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:14:45 +0000 Subject: [PATCH 1/4] process: start agent CLIs in a new session so Ctrl-C ends them at once ## Changes `process.BackgroundInGroup` now starts the child as the leader of a new session (`Setsid`) instead of only putting it in a new process group (`Setpgid`). The aitools installer and agent detection use this helper when they run agent CLIs such as `claude plugin ...`. ```diff BackgroundInGroup(ctx, args) - Background(ctx, args, WithProcessGroup()) + Background(ctx, args, WithProcessGroup(), withNewSession()) ``` - `withNewSession` (in `libs/process/interrupt_unix.go`) must come after `WithProcessGroup`. It clears `Setpgid` and sets `Setsid`, because `setpgid` fails for a process that is already a session leader. A session leader's group ID equals its PID, so the existing group signals (`kill(-pid, ...)`) still reach the child and all of its descendants. - `reapProcessGroup` (in `libs/process/group_unix.go`) now also runs for a `Setsid` child. Without this, the final group `SIGKILL` after the grace period would be skipped, and a descendant that ignores `SIGTERM` would be left running. ## Why `BackgroundInGroup` moves the child out of the terminal's foreground group. When the user presses Ctrl-C, the CLI stops the group with `SIGTERM` and then ends itself the way the interrupt would have. Some agent CLIs (Claude Code is one) read the terminal when they get `SIGTERM`. A process in a background group of the terminal's session that reads the terminal gets `SIGTTIN` and stops. The child then sat stopped until the `SIGKILL` at the end of the 10s grace period, so Ctrl-C during an AI tools install took about 10 seconds to take effect. A child in its own session has no controlling terminal: its `open("/dev/tty")` fails at once, it exits, and Ctrl-C ends the CLI right away. ## Tests - New `TestBackgroundInGroupInterruptChildReadsTerminal` (Linux only, since it opens a pseudo-terminal with platform-specific ioctls). The test starts itself as a helper process in the foreground group of a fresh pty. The helper runs `BackgroundInGroup` on a shell that, on `SIGTERM`, reads `/dev/tty` before it exits. The test then sends `SIGINT` to the helper and checks that the helper is ended by `SIGINT` within half the grace period. Before this change the child was stopped by `SIGTTIN` and the helper outlived that deadline. - New `TestBackgroundInGroupReapsGrandchildAfterEscalation`: the child ignores `SIGTERM` and has a grandchild. After the context is cancelled, the test checks that the escalation `SIGKILL` reaches the grandchild. This covers the `reapProcessGroup` change for session leaders. - The existing `libs/process` tests pass, including `TestBackgroundInGroupForwardsInterrupt`. Co-authored-by: Isaac --- libs/process/group_unix.go | 2 +- libs/process/interrupt_linux_test.go | 87 ++++++++++++++++++++++++++++ libs/process/interrupt_unix.go | 21 ++++++- libs/process/interrupt_unix_test.go | 34 +++++++++++ 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 libs/process/interrupt_linux_test.go diff --git a/libs/process/group_unix.go b/libs/process/group_unix.go index 8f0adb225aa..0d860b94312 100644 --- a/libs/process/group_unix.go +++ b/libs/process/group_unix.go @@ -61,7 +61,7 @@ func WithProcessGroup() execOption { // or returns ESRCH on an already-empty group; there is no 10s window in which the // PGID could be reused by an unrelated group before the signal is sent. func reapProcessGroup(ctx context.Context, c *exec.Cmd) { - if c.SysProcAttr == nil || !c.SysProcAttr.Setpgid { + if c.SysProcAttr == nil || (!c.SysProcAttr.Setpgid && !c.SysProcAttr.Setsid) { return } if c.Process == nil || ctx.Err() == nil { diff --git a/libs/process/interrupt_linux_test.go b/libs/process/interrupt_linux_test.go new file mode 100644 index 00000000000..54a579455c3 --- /dev/null +++ b/libs/process/interrupt_linux_test.go @@ -0,0 +1,87 @@ +package process + +import ( + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "syscall" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" +) + +// terminalHelperEnv makes TestBackgroundInGroupInterruptChildReadsTerminal +// run BackgroundInGroup in a helper process that has a terminal, which the +// test interrupts. +const terminalHelperEnv = "PROCESS_TEST_TERMINAL_HELPER_PIDFILE" + +// runTerminalHelper is the helper process side of +// TestBackgroundInGroupInterruptChildReadsTerminal. +func runTerminalHelper(t *testing.T, pidFile string) { + // On SIGTERM the child reads the terminal before it exits. + script := `trap 'read -r line "$1"; wait` + _, _ = BackgroundInGroup(t.Context(), []string{"sh", "-c", script, "sh", pidFile}) + // The raised signal ends the process asynchronously. + time.Sleep(5 * time.Second) + t.Fatal("the interrupt did not end the process") +} + +// TestBackgroundInGroupInterruptChildReadsTerminal covers a child that reads +// the terminal once it got SIGTERM, as Claude Code does. It is Linux only +// because opening a pseudo-terminal takes platform-specific ioctls. +func TestBackgroundInGroupInterruptChildReadsTerminal(t *testing.T) { + if pidFile := os.Getenv(terminalHelperEnv); pidFile != "" { + runTerminalHelper(t, pidFile) + return + } + ptmx, tty := openPty(t) + pidFile := filepath.Join(t.TempDir(), "grandchild.pid") + helper := exec.Command(os.Args[0], "-test.run=^TestBackgroundInGroupInterruptChildReadsTerminal$") + helper.Env = append(os.Environ(), terminalHelperEnv+"="+pidFile) + helper.Stdin, helper.Stdout, helper.Stderr = tty, tty, tty + // Like a CLI started from a shell, the helper is in the foreground group of + // its controlling terminal. + helper.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true, Ctty: 0} + require.NoError(t, helper.Start()) + require.NoError(t, tty.Close()) + // Drain the terminal so the helper never blocks writing to it. + go func() { _, _ = io.Copy(io.Discard, ptmx) }() + + waitForPid(t, pidFile) + require.NoError(t, helper.Process.Signal(os.Interrupt)) + + waitErr := make(chan error, 1) + go func() { waitErr <- helper.Wait() }() + // A child stopped by SIGTTIN would hold the helper until the SIGKILL at the + // end of the grace period. + select { + case err := <-waitErr: + exitErr, ok := errors.AsType[*exec.ExitError](err) + require.True(t, ok, "helper exited with %v", err) + status := exitErr.Sys().(syscall.WaitStatus) + assert.True(t, status.Signaled(), "the helper must end as the interrupt would have, got %v", err) + assert.Equal(t, syscall.SIGINT, status.Signal()) + case <-time.After(processGroupGracePeriod / 2): + _ = helper.Process.Kill() + t.Fatal("the interrupt did not end the helper before the grace period") + } +} + +// openPty opens a pseudo-terminal and returns its master and slave ends. +func openPty(t *testing.T) (ptmx, tty *os.File) { + ptmx, err := os.OpenFile("/dev/ptmx", os.O_RDWR|syscall.O_NOCTTY, 0) + require.NoError(t, err) + t.Cleanup(func() { _ = ptmx.Close() }) + require.NoError(t, unix.IoctlSetPointerInt(int(ptmx.Fd()), unix.TIOCSPTLCK, 0)) + n, err := unix.IoctlGetUint32(int(ptmx.Fd()), unix.TIOCGPTN) + require.NoError(t, err) + tty, err = os.OpenFile(fmt.Sprintf("/dev/pts/%d", n), os.O_RDWR|syscall.O_NOCTTY, 0) + require.NoError(t, err) + return ptmx, tty +} diff --git a/libs/process/interrupt_unix.go b/libs/process/interrupt_unix.go index 26fd8ffdbd3..53d250ee88b 100644 --- a/libs/process/interrupt_unix.go +++ b/libs/process/interrupt_unix.go @@ -5,6 +5,7 @@ package process import ( "context" "os" + "os/exec" "os/signal" "syscall" ) @@ -18,6 +19,12 @@ import ( // in this process, which ends as it would have without the group. The default // disposition is restored before the group is stopped, so a second signal ends // this process at once. +// +// The child also leads a new session, so it has no controlling terminal. In a +// background group of the terminal's session, a child that reads the terminal +// once it got SIGTERM (Claude Code does) is stopped by SIGTTIN until the +// SIGKILL at the end of the grace period. Without a controlling terminal its +// open of /dev/tty fails instead. func BackgroundInGroup(ctx context.Context, args []string, opts ...execOption) (string, error) { ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -37,7 +44,7 @@ func BackgroundInGroup(ctx context.Context, args []string, opts ...execOption) ( } }() - out, err := Background(ctx, args, append(opts, WithProcessGroup())...) + out, err := Background(ctx, args, append(opts, WithProcessGroup(), withNewSession())...) close(done) signal.Stop(sigCh) if sig, ok := <-caught; ok { @@ -45,3 +52,15 @@ func BackgroundInGroup(ctx context.Context, args []string, opts ...execOption) ( } return out, err } + +// withNewSession makes the child of WithProcessGroup, which it must follow, the +// leader of a new session instead. setpgid fails for a session leader, so it +// clears Setpgid; a session leader's group ID is its PID, so the group signals +// of WithProcessGroup still reach the child and its descendants. +func withNewSession() execOption { + return func(_ context.Context, c *exec.Cmd) error { + c.SysProcAttr.Setpgid = false + c.SysProcAttr.Setsid = true + return nil + } +} diff --git a/libs/process/interrupt_unix_test.go b/libs/process/interrupt_unix_test.go index 090a3477b1b..4475a0803f8 100644 --- a/libs/process/interrupt_unix_test.go +++ b/libs/process/interrupt_unix_test.go @@ -3,6 +3,7 @@ package process import ( + "context" "errors" "os" "os/exec" @@ -60,3 +61,36 @@ func TestBackgroundInGroupForwardsInterrupt(t *testing.T) { return errors.Is(syscall.Kill(grandchildPid, 0), syscall.ESRCH) }, 5*time.Second, 20*time.Millisecond, "grandchild %d outlived the interrupt", grandchildPid) } + +// TestBackgroundInGroupReapsGrandchildAfterEscalation covers the SIGKILL +// escalation for a child that leads its own session: the final group SIGKILL +// must reach the grandchild of a leader that ignores SIGTERM. +func TestBackgroundInGroupReapsGrandchildAfterEscalation(t *testing.T) { + orig := processGroupGracePeriod + processGroupGracePeriod = 500 * time.Millisecond + t.Cleanup(func() { processGroupGracePeriod = orig }) + + ctx, cancel := context.WithCancel(t.Context()) + pidFile := filepath.Join(t.TempDir(), "grandchild.pid") + script := []string{"sh", "-c", `trap '' TERM; sleep 300 & echo $! > "$1"; wait`, "sh", pidFile} + + done := make(chan struct{}) + go func() { + defer close(done) + _, _ = BackgroundInGroup(ctx, script) + }() + + grandchildPid := waitForPid(t, pidFile) + cancel() + + select { + case <-done: + case <-time.After(processGroupGracePeriod + 10*time.Second): + t.Fatal("BackgroundInGroup did not return after escalation") + } + + assert.Eventually(t, func() bool { + return errors.Is(syscall.Kill(grandchildPid, 0), syscall.ESRCH) + }, 5*time.Second, 20*time.Millisecond, + "grandchild %d survived the SIGKILL escalation (re-orphaned)", grandchildPid) +} From 3eeb7ede4b475bb9e060afeb4dd39386c3b43ff5 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:05:19 +0000 Subject: [PATCH 2/4] process: describe session leaders in the process group comments Co-authored-by: Isaac --- libs/process/group_unix.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libs/process/group_unix.go b/libs/process/group_unix.go index 0d860b94312..1aab6f55aee 100644 --- a/libs/process/group_unix.go +++ b/libs/process/group_unix.go @@ -35,8 +35,9 @@ func WithProcessGroup() execOption { c.WaitDelay = processGroupGracePeriod c.Cancel = func() error { - // With Setpgid and Pgid unset, the child's group ID equals its PID; - // a negative PID targets the whole group. Map "no such process" to + // With Setpgid and Pgid unset, or with Setsid (see withNewSession), + // the child's group ID equals its PID; a negative PID targets the + // whole group. Map "no such process" to // os.ErrProcessDone so a benign exit/cancel race is not surfaced as a // Wait error. err := syscall.Kill(-c.Process.Pid, syscall.SIGTERM) @@ -51,9 +52,10 @@ func WithProcessGroup() execOption { // reapProcessGroup sends a final SIGKILL to the child's process group after the // command has been waited on, closing the gap left by Go's WaitDelay escalation -// (which SIGKILLs only the leader PID). It runs only for a WithProcessGroup child -// whose context was cancelled — the escalation path — so a normally-exited -// command is never signalled. +// (which SIGKILLs only the leader PID). It runs only for a WithProcessGroup child, +// a group leader through Setpgid or a session leader through Setsid, whose +// context was cancelled — the escalation path — so a normally-exited command is +// never signalled. // // It is safe against PID reuse: this runs synchronously after Wait has returned // (the leader is reaped), not on a delayed timer. The group ID stays reserved by From eb9f01b64725840111ac5303ef782bd15f689acc Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:57:04 +0000 Subject: [PATCH 3/4] process: address review comments Add a changelog entry for the Ctrl-C fix, run the SIGKILL escalation test over both runners in one table-driven test, and reword the group signal comment. Co-authored-by: Isaac --- .nextchanges/cli/7832.md | 1 + libs/process/group_unix.go | 6 +- libs/process/group_unix_test.go | 87 ++++++++++++++++++----------- libs/process/interrupt_unix_test.go | 34 ----------- 4 files changed, 59 insertions(+), 69 deletions(-) create mode 100644 .nextchanges/cli/7832.md diff --git a/.nextchanges/cli/7832.md b/.nextchanges/cli/7832.md new file mode 100644 index 00000000000..c790e4df5a7 --- /dev/null +++ b/.nextchanges/cli/7832.md @@ -0,0 +1 @@ +* Fix Ctrl-C taking about 10 seconds to stop `databricks aitools install` while it runs a coding agent's CLI. ([#7832](https://github.com/databricks/cli/pull/7832)) diff --git a/libs/process/group_unix.go b/libs/process/group_unix.go index 1aab6f55aee..9c6f3f66cbd 100644 --- a/libs/process/group_unix.go +++ b/libs/process/group_unix.go @@ -35,9 +35,9 @@ func WithProcessGroup() execOption { c.WaitDelay = processGroupGracePeriod c.Cancel = func() error { - // With Setpgid and Pgid unset, or with Setsid (see withNewSession), - // the child's group ID equals its PID; a negative PID targets the - // whole group. Map "no such process" to + // The child leads its own process group (Setpgid with Pgid 0, or + // Setsid; see withNewSession), so its group ID equals its PID and a + // negative PID targets the whole group. Map "no such process" to // os.ErrProcessDone so a benign exit/cancel race is not surfaced as a // Wait error. err := syscall.Kill(-c.Process.Pid, syscall.SIGTERM) diff --git a/libs/process/group_unix_test.go b/libs/process/group_unix_test.go index 3db07a9bac9..a0b29f03fa1 100644 --- a/libs/process/group_unix_test.go +++ b/libs/process/group_unix_test.go @@ -57,40 +57,63 @@ func TestWithProcessGroupReapsGrandchild(t *testing.T) { // SIGTERM from Cancel does nothing, so WaitDelay expires and Go SIGKILLs the // leader PID only — leaving the grandchild that this option exists to reap. The // post-Wait group sweep (reapProcessGroup) must SIGKILL the whole group so the -// grandchild does not survive. +// grandchild does not survive. It runs for a child that leads its own process +// group (Setpgid) and for one that leads its own session (Setsid, through +// BackgroundInGroup). func TestWithProcessGroupReapsGrandchildAfterEscalation(t *testing.T) { - // Shorten the grace period so the WaitDelay escalation fires quickly. - orig := processGroupGracePeriod - processGroupGracePeriod = 500 * time.Millisecond - t.Cleanup(func() { processGroupGracePeriod = orig }) - - ctx, cancel := context.WithCancel(t.Context()) - - pidFile := filepath.Join(t.TempDir(), "grandchild.pid") - // The leader ignores SIGTERM, so only the escalation can stop it; the sleep - // grandchild inherits the group but not the trap. - script := []string{"sh", "-c", `trap '' TERM; sleep 300 & echo $! > "$1"; wait`, "sh", pidFile} - - done := make(chan struct{}) - go func() { - defer close(done) - _, _ = Background(ctx, script, WithProcessGroup()) - }() - - grandchildPid := waitForPid(t, pidFile) - - cancel() - - select { - case <-done: - case <-time.After(processGroupGracePeriod + 10*time.Second): - t.Fatal("Background did not return after escalation") + tests := []struct { + name string + run func(ctx context.Context, args []string) (string, error) + }{ + { + name: "process group", + run: func(ctx context.Context, args []string) (string, error) { + return Background(ctx, args, WithProcessGroup()) + }, + }, + { + name: "session", + run: func(ctx context.Context, args []string) (string, error) { + return BackgroundInGroup(ctx, args) + }, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Shorten the grace period so the WaitDelay escalation fires quickly. + orig := processGroupGracePeriod + processGroupGracePeriod = 500 * time.Millisecond + t.Cleanup(func() { processGroupGracePeriod = orig }) + + ctx, cancel := context.WithCancel(t.Context()) + + pidFile := filepath.Join(t.TempDir(), "grandchild.pid") + // The leader ignores SIGTERM, so only the escalation can stop it; the sleep + // grandchild inherits the group but not the trap. + script := []string{"sh", "-c", `trap '' TERM; sleep 300 & echo $! > "$1"; wait`, "sh", pidFile} + + done := make(chan struct{}) + go func() { + defer close(done) + _, _ = tc.run(ctx, script) + }() + + grandchildPid := waitForPid(t, pidFile) + + cancel() + + select { + case <-done: + case <-time.After(processGroupGracePeriod + 10*time.Second): + t.Fatal("the runner did not return after escalation") + } + + assert.Eventually(t, func() bool { + return errors.Is(syscall.Kill(grandchildPid, 0), syscall.ESRCH) + }, 5*time.Second, 20*time.Millisecond, + "grandchild %d survived the SIGKILL escalation (re-orphaned)", grandchildPid) + }) } - - assert.Eventually(t, func() bool { - return errors.Is(syscall.Kill(grandchildPid, 0), syscall.ESRCH) - }, 5*time.Second, 20*time.Millisecond, - "grandchild %d survived the SIGKILL escalation (re-orphaned)", grandchildPid) } // waitForPid waits for the shell to write the grandchild PID and returns it. diff --git a/libs/process/interrupt_unix_test.go b/libs/process/interrupt_unix_test.go index 4475a0803f8..090a3477b1b 100644 --- a/libs/process/interrupt_unix_test.go +++ b/libs/process/interrupt_unix_test.go @@ -3,7 +3,6 @@ package process import ( - "context" "errors" "os" "os/exec" @@ -61,36 +60,3 @@ func TestBackgroundInGroupForwardsInterrupt(t *testing.T) { return errors.Is(syscall.Kill(grandchildPid, 0), syscall.ESRCH) }, 5*time.Second, 20*time.Millisecond, "grandchild %d outlived the interrupt", grandchildPid) } - -// TestBackgroundInGroupReapsGrandchildAfterEscalation covers the SIGKILL -// escalation for a child that leads its own session: the final group SIGKILL -// must reach the grandchild of a leader that ignores SIGTERM. -func TestBackgroundInGroupReapsGrandchildAfterEscalation(t *testing.T) { - orig := processGroupGracePeriod - processGroupGracePeriod = 500 * time.Millisecond - t.Cleanup(func() { processGroupGracePeriod = orig }) - - ctx, cancel := context.WithCancel(t.Context()) - pidFile := filepath.Join(t.TempDir(), "grandchild.pid") - script := []string{"sh", "-c", `trap '' TERM; sleep 300 & echo $! > "$1"; wait`, "sh", pidFile} - - done := make(chan struct{}) - go func() { - defer close(done) - _, _ = BackgroundInGroup(ctx, script) - }() - - grandchildPid := waitForPid(t, pidFile) - cancel() - - select { - case <-done: - case <-time.After(processGroupGracePeriod + 10*time.Second): - t.Fatal("BackgroundInGroup did not return after escalation") - } - - assert.Eventually(t, func() bool { - return errors.Is(syscall.Kill(grandchildPid, 0), syscall.ESRCH) - }, 5*time.Second, 20*time.Millisecond, - "grandchild %d survived the SIGKILL escalation (re-orphaned)", grandchildPid) -} From 25f98fdc1e5a08529e7bf84156a449602f5be280 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Fri, 20 Nov 2026 15:17:01 +0000 Subject: [PATCH 4/4] process: clean up after failed interrupt tests and widen the changelog entry Kill the helper and its child's group when the terminal test fails, log the helper's terminal output, and check that the grandchild was stopped. Kill the escalation test's grandchild on failure and fix the comment about the ignored SIGTERM it inherits. Name update and uninstall in the changelog entry too. Co-authored-by: Isaac --- .nextchanges/cli/7832.md | 2 +- libs/process/group_unix_test.go | 7 +++-- libs/process/interrupt_linux_test.go | 43 +++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/.nextchanges/cli/7832.md b/.nextchanges/cli/7832.md index c790e4df5a7..832e945bed3 100644 --- a/.nextchanges/cli/7832.md +++ b/.nextchanges/cli/7832.md @@ -1 +1 @@ -* Fix Ctrl-C taking about 10 seconds to stop `databricks aitools install` while it runs a coding agent's CLI. ([#7832](https://github.com/databricks/cli/pull/7832)) +* Fix Ctrl-C taking about 10 seconds to stop `databricks aitools install`, `update` or `uninstall` while it runs a coding agent's CLI. ([#7832](https://github.com/databricks/cli/pull/7832)) diff --git a/libs/process/group_unix_test.go b/libs/process/group_unix_test.go index a0b29f03fa1..2a0a78f2c72 100644 --- a/libs/process/group_unix_test.go +++ b/libs/process/group_unix_test.go @@ -88,8 +88,9 @@ func TestWithProcessGroupReapsGrandchildAfterEscalation(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) pidFile := filepath.Join(t.TempDir(), "grandchild.pid") - // The leader ignores SIGTERM, so only the escalation can stop it; the sleep - // grandchild inherits the group but not the trap. + // The leader ignores SIGTERM, so only the escalation can stop it. The sleep + // grandchild inherits the group and the ignored SIGTERM, so only the group + // sweep can stop it. script := []string{"sh", "-c", `trap '' TERM; sleep 300 & echo $! > "$1"; wait`, "sh", pidFile} done := make(chan struct{}) @@ -99,6 +100,8 @@ func TestWithProcessGroupReapsGrandchildAfterEscalation(t *testing.T) { }() grandchildPid := waitForPid(t, pidFile) + // Do not leave the grandchild running for 5 minutes if the sweep fails. + t.Cleanup(func() { _ = syscall.Kill(grandchildPid, syscall.SIGKILL) }) cancel() diff --git a/libs/process/interrupt_linux_test.go b/libs/process/interrupt_linux_test.go index 54a579455c3..8620cb5de62 100644 --- a/libs/process/interrupt_linux_test.go +++ b/libs/process/interrupt_linux_test.go @@ -1,6 +1,7 @@ package process import ( + "bytes" "errors" "fmt" "io" @@ -48,12 +49,43 @@ func TestBackgroundInGroupInterruptChildReadsTerminal(t *testing.T) { // Like a CLI started from a shell, the helper is in the foreground group of // its controlling terminal. helper.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true, Ctty: 0} + + // Read the terminal so the helper never blocks writing to it, and keep what + // it wrote to report it if the test fails. The copy ends once the helper + // exits and the terminal has no other process left. + var out bytes.Buffer + outDone := make(chan struct{}) + go func() { + defer close(outDone) + _, _ = io.Copy(&out, ptmx) + }() + output := func() string { + select { + case <-outDone: + return out.String() + case <-time.After(time.Second): + return "(the helper's terminal is still open)" + } + } + require.NoError(t, helper.Start()) + // The helper leads its own session; kill it if the test fails before it ends. + t.Cleanup(func() { + _ = syscall.Kill(-helper.Process.Pid, syscall.SIGKILL) + if t.Failed() { + t.Logf("helper output:\n%s", output()) + } + }) require.NoError(t, tty.Close()) - // Drain the terminal so the helper never blocks writing to it. - go func() { _, _ = io.Copy(io.Discard, ptmx) }() - waitForPid(t, pidFile) + grandchildPid := waitForPid(t, pidFile) + // The child of BackgroundInGroup leads a session of its own, so killing the + // helper does not stop it; kill its group if the test fails before it ends. + childPgid, err := syscall.Getpgid(grandchildPid) + require.NoError(t, err) + require.NotEqual(t, syscall.Getpgrp(), childPgid, "the child must not share the test's process group") + t.Cleanup(func() { _ = syscall.Kill(-childPgid, syscall.SIGKILL) }) + require.NoError(t, helper.Process.Signal(os.Interrupt)) waitErr := make(chan error, 1) @@ -68,9 +100,12 @@ func TestBackgroundInGroupInterruptChildReadsTerminal(t *testing.T) { assert.True(t, status.Signaled(), "the helper must end as the interrupt would have, got %v", err) assert.Equal(t, syscall.SIGINT, status.Signal()) case <-time.After(processGroupGracePeriod / 2): - _ = helper.Process.Kill() t.Fatal("the interrupt did not end the helper before the grace period") } + + assert.Eventually(t, func() bool { + return errors.Is(syscall.Kill(grandchildPid, 0), syscall.ESRCH) + }, 5*time.Second, 20*time.Millisecond, "grandchild %d outlived the interrupt", grandchildPid) } // openPty opens a pseudo-terminal and returns its master and slave ends.