diff --git a/.nextchanges/cli/7832.md b/.nextchanges/cli/7832.md new file mode 100644 index 00000000000..832e945bed3 --- /dev/null +++ b/.nextchanges/cli/7832.md @@ -0,0 +1 @@ +* 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.go b/libs/process/group_unix.go index 8f0adb225aa..9c6f3f66cbd 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 + // 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) @@ -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 @@ -61,7 +63,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/group_unix_test.go b/libs/process/group_unix_test.go index 3db07a9bac9..2a0a78f2c72 100644 --- a/libs/process/group_unix_test.go +++ b/libs/process/group_unix_test.go @@ -57,40 +57,66 @@ 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 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{}) + go func() { + defer close(done) + _, _ = tc.run(ctx, script) + }() + + 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() + + 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_linux_test.go b/libs/process/interrupt_linux_test.go new file mode 100644 index 00000000000..8620cb5de62 --- /dev/null +++ b/libs/process/interrupt_linux_test.go @@ -0,0 +1,122 @@ +package process + +import ( + "bytes" + "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} + + // 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()) + + 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) + 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): + 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. +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 + } +}