Repository navigation
Group-kill agent CLI process trees when a run is stopped - #226
Merged
currentbits merged 4 commits intoAug 17, 2026
Merged
Conversation
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Co-Authored-By: Claude Opus 5 <[email protected]>
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
force-pushed
the
coder/stopping-a-run-leaves-agent-grandchildre-0e1dba
branch
from
August 17, 2026 09:43
1186815 to
746cdfc
Compare
currentbits
deleted the
coder/stopping-a-run-leaves-agent-grandchildre-0e1dba
branch
October 3, 2026 16:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #143.
Problem
kill()on a run SIGTERMed only the direct agent-CLI child. The CLIs were not spawneddetached, so they were not process-group leaders and there was nokill(-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.jsexportskillTree(child, ms): SIGTERM the child's process group viaprocess.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 shapedevservers.js:149-175already used correctly.All five agent-CLI spawns (
agent.js,claude.js,codex.js,kimi.js,opencode.js) now passdetached: true, and their five byte-identical 14-linekill()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, IPCruns:stop, web clients — routes through a handle'skill(), so all are covered by the adapter change.Ctrl-C regression that
detachedintroduces, and its fix. Ctrl-C onnpm run devused to reach the CLIs because they shared the terminal's foreground process group. Once detached they are immune, andmain.jshad no SIGINT/SIGTERM handler — onlybefore-quit, which a default-disposition SIGTERM skips entirely, so every running CLI group would have survived. Newelectron/shutdown.js(installShutdown, same shape as the existinginstallCrashGuard) givesbefore-quitand both signals one re-entrant teardown, thenprocess.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'snohup'd process still survives, asrunner.js:316documents deliberately.Verification
Both new tests were mutation-checked rather than just run green:
electron/test/proc.test.jsbackgrounds a real grandchild, kills, and asserts it dies. MutatingkillTreeto signal only the direct child makes it fail.electron/test/shutdown.test.jsasserts registration on the realprocessobject. Moving the handler to SIGHUP makes 4 of 5 assertions fail.tsc --noEmitclean. Full electron suite: 817 pass, 11 fail — those 11 (orch-server,web-integration) fail identically onmainin 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 devagainst 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, sinceprocess.exit(0)does not wait for the 3s SIGKILL escalation. Worth a detached reaper only if one is observed lingering in practice.