diff --git a/docs/changes/unreleased/2950-seniordev-keep-shell-processes.md b/docs/changes/unreleased/2950-seniordev-keep-shell-processes.md new file mode 100644 index 0000000000..c80f11fdfd --- /dev/null +++ b/docs/changes/unreleased/2950-seniordev-keep-shell-processes.md @@ -0,0 +1,8 @@ +--- +kind: added +title: SENIOR_DEV_KEEP_SHELL_PROCESSES=1 leaves a senior-dev run's processes running when it ends +pr: 2950 +surface: [engine] +invalidates: [] +--- +By default senior-dev stops every process its run started. With `SENIOR_DEV_KEEP_SHELL_PROCESSES=1`, background jobs and other descendants keep running after the run ends, so a service the request asked to be left up stays up. diff --git a/internal/processgroup/run_descendants_linux.go b/internal/processgroup/run_descendants_linux.go index 96fca77f4a..279d80ee1a 100644 --- a/internal/processgroup/run_descendants_linux.go +++ b/internal/processgroup/run_descendants_linux.go @@ -16,6 +16,12 @@ import ( const RunMarkerEnv = "CODEAF_DELEGATE_RUN" +// keepEnv=1 leaves a run's processes running when it ends, so a service the +// request asked to be left up outlives the run (#2901). +const keepEnv = "SENIOR_DEV_KEEP_SHELL_PROCESSES" + +func keepProcesses() bool { return os.Getenv(keepEnv) == "1" } + // EnableSubreaper keeps an orphaned shell descendant with the engine after // the shell exits, so this run can kill and reap it before releasing its folder. func EnableSubreaper() error { @@ -29,6 +35,9 @@ func EnableSubreaper() error { // run's child subreaper. Its process tree includes detached and environment- // sanitized shell children, but no process started by the chat or a sibling run. func CleanupDescendants() { + if keepProcesses() { + return + } self := os.Getpid() empty := 0 for pass := 0; pass < 40; pass++ { @@ -57,6 +66,9 @@ func CleanupDescendants() { // after SIGKILL the engine cannot do that walk, so the private launch marker // can find only descendants that kept it in their environment. func CleanupRun(marker string) { + if keepProcesses() { + return + } // A setsid wrapper can fork just as the engine exits. A short settle // window lets its final exec inherit the marker before declaring it gone. empty := 0 diff --git a/internal/processgroup/run_descendants_linux_test.go b/internal/processgroup/run_descendants_linux_test.go new file mode 100644 index 0000000000..5ff098d361 --- /dev/null +++ b/internal/processgroup/run_descendants_linux_test.go @@ -0,0 +1,28 @@ +//go:build linux + +package processgroup + +import ( + "os/exec" + "syscall" + "testing" + "time" +) + +func TestCleanupDescendantsLeavesProcessesWhenTheRunKeepsThem(t *testing.T) { + t.Setenv("SENIOR_DEV_KEEP_SHELL_PROCESSES", "1") + service := exec.Command("sleep", "300") + if err := service.Start(); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = service.Process.Kill(); _ = service.Wait() }) + CleanupDescendants() + time.Sleep(50 * time.Millisecond) + if err := syscall.Kill(service.Process.Pid, 0); err != nil { + t.Fatalf("descendant %d was killed although the run keeps its shell processes: %v", service.Process.Pid, err) + } + var status syscall.WaitStatus + if pid, _ := syscall.Wait4(service.Process.Pid, &status, syscall.WNOHANG, nil); pid == service.Process.Pid { + t.Fatalf("descendant %d exited (%v) although the run keeps its shell processes", pid, status) + } +} diff --git a/internal/seniordev/tool/bash.go b/internal/seniordev/tool/bash.go index 67d3b23969..ce2a1f538e 100644 --- a/internal/seniordev/tool/bash.go +++ b/internal/seniordev/tool/bash.go @@ -296,10 +296,16 @@ func (r *Registry) pruneShellGroup(pid int) { // CloseShellProcesses ends background jobs left in the process groups that // the run's bash tool started before the run releases its workspace. +// +// SENIOR_DEV_KEEP_SHELL_PROCESSES=1 leaves them running, so a service the +// request asked to be left up outlives the run (#2901). func (r *Registry) CloseShellProcesses() { processes := r.shellProcesses processes.mu.Lock() defer processes.mu.Unlock() + if os.Getenv("SENIOR_DEV_KEEP_SHELL_PROCESSES") == "1" { + return + } processes.closed = true for pid := range processes.groups { killProcessGroup(pid) diff --git a/internal/seniordev/tool/bash_background_test.go b/internal/seniordev/tool/bash_background_test.go index 183bd5c49c..a76b870145 100644 --- a/internal/seniordev/tool/bash_background_test.go +++ b/internal/seniordev/tool/bash_background_test.go @@ -60,3 +60,30 @@ func shellProcessRunning(pid int) bool { fields := strings.Fields(string(raw)) return len(fields) > 2 && fields[2] != "Z" } + +func TestBackgroundBashOutlivesTheRunWhenTheRunKeepsShellProcesses(t *testing.T) { + t.Setenv("SENIOR_DEV_KEEP_SHELL_PROCESSES", "1") + registry := New(t.TempDir()) + pidFile := filepath.Join(t.TempDir(), "pid") + input, _ := json.Marshal(map[string]any{ + "command": "sleep 300 & echo $! > " + pidFile, + "timeout_ms": 500, + }) + if _, err := registry.Execute(context.Background(), steploop.ToolCall{ID: "service", Name: "bash", Input: input}); err != nil { + t.Fatal(err) + } + raw, err := os.ReadFile(pidFile) + if err != nil { + t.Fatal(err) + } + pid, err := strconv.Atoi(strings.TrimSpace(string(raw))) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = syscall.Kill(pid, syscall.SIGKILL) }) + registry.CloseShellProcesses() + time.Sleep(200 * time.Millisecond) + if !shellProcessRunning(pid) { + t.Fatalf("background process %d was killed although the run keeps its shell processes", pid) + } +}