Skip to content

Group-kill agent CLI process trees when a run is stopped - #226

Merged
currentbits merged 4 commits into
mainfrom
coder/stopping-a-run-leaves-agent-grandchildre-0e1dba
Aug 17, 2026
Merged

currentbits merged 4 commits into
mainfrom
coder/stopping-a-run-leaves-agent-grandchildre-0e1dba

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Fixes #143.

Problem

kill() on a run SIGTERMed only the direct agent-CLI child. The CLIs were not spawned detached, so they were not process-group leaders and there was no kill(-pid) teardown. Anything the agent started through its shell tool — test watchers, servers, build steps — was orphaned on stop, kept burning CPU, and held the worktree cwd open.

What changed

Group kill. New electron/proc.js exports killTree(child, ms): SIGTERM the child's process group via process.kill(-pid), fall back to the bare pid if the group signal throws, then escalate to SIGKILL on an unref'd timer. This is the shape devservers.js:149-175 already used correctly.

All five agent-CLI spawns (agent.js, claude.js, codex.js, kimi.js, opencode.js) now pass detached: true, and their five byte-identical 14-line kill() bodies collapse to a single call each. runner.js's app-quit reaper group-kills as well, and now escalates to SIGKILL where it previously sent a lone SIGTERM with no fallback.

Every stop path — stopRun, stopCrew, stopAll, disposeClaudeSession, stopWorkflowEntry, IPC runs:stop, web clients — routes through a handle's kill(), so all are covered by the adapter change.

Ctrl-C regression that detached introduces, and its fix. Ctrl-C on npm run dev used to reach the CLIs because they shared the terminal's foreground process group. Once detached they are immune, and main.js had no SIGINT/SIGTERM handler — only before-quit, which a default-disposition SIGTERM skips entirely, so every running CLI group would have survived. New electron/shutdown.js (installShutdown, same shape as the existing installCrashGuard) gives before-quit and both signals one re-entrant teardown, then process.exit(0).

No allowlist, deliberately

Issue #143 floated an allowlist for processes the devservers feature claims. It is not needed: devservers are spawned in the Electron main process (ipc.js:713 → devservers.js:194), already detached into their own process group. They are never descendants of an agent CLI, so an agent group-kill cannot reach them, and no MCP or bridge path makes one a descendant. A dev server the agent starts itself in its shell is in the agent group and now dies on stop — the behaviour the issue asked for. An agent's nohup'd process still survives, as runner.js:316 documents deliberately.

Verification

Both new tests were mutation-checked rather than just run green:

  • electron/test/proc.test.js backgrounds a real grandchild, kills, and asserts it dies. Mutating killTree to signal only the direct child makes it fail.
  • electron/test/shutdown.test.js asserts registration on the real process object. Moving the handler to SIGHUP makes 4 of 5 assertions fail.

tsc --noEmit clean. Full electron suite: 817 pass, 11 fail — those 11 (orch-server, web-integration) fail identically on main in this sandbox because the test servers cannot bind, so they are pre-existing and unrelated.

Not exercised: an end-to-end Ctrl-C of npm run dev against a live GUI run. The signal handler is unit-tested, but the real Electron path was not driven. Related residual: after Ctrl-C, a SIGTERM-trapping CLI can still survive, since process.exit(0) does not wait for the 3s SIGKILL escalation. Worth a detached reaper only if one is observed lingering in practice.

  • PR created by the Claude Code agent

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Willem and others added 4 commits August 17, 2026 11:43
Spawn agent CLIs detached so they are process-group leaders, and
SIGTERM/SIGKILL the group via a shared killTree helper. Stopping a
run now reaps shell-tool grandchildren instead of orphaning them.

Devservers stay untouched: they already spawn in their own group
from the Electron main process, so they are not in the agent tree.
detached:true agent CLIs no longer die with the terminal process group.
SIGINT/SIGTERM now share the before-quit teardown (re-entrant) and
process.exit(0) after stopAll has SIGTERM'd the groups.
The fake-emitter harness proved the re-entrancy guard but nothing about
signal wiring: moving the handler to SIGHUP still passed. Register on the
real process (as crash-guard.js does) and restore the default disposition
after each test. Drops the proc injection that only existed for the fake.

Co-Authored-By: Claude Opus 5 <[email protected]>
@currentbits
currentbits force-pushed the coder/stopping-a-run-leaves-agent-grandchildre-0e1dba branch from 1186815 to 746cdfc Compare August 17, 2026 09:43
@currentbits
currentbits merged commit 9f5decb into main Aug 17, 2026
1 check failed
@currentbits
currentbits deleted the coder/stopping-a-run-leaves-agent-grandchildre-0e1dba branch October 3, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stopping a run leaves agent grandchildren alive (no process-group kill)

1 participant