Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions internal/processgroup/run_descendants_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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++ {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions internal/processgroup/run_descendants_linux_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions internal/seniordev/tool/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions internal/seniordev/tool/bash_background_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading