Skip to content

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

Closed

Description

@currentbits

Problem

electron/claude.js:321-337 (same shape in kimi.js:524-542 and the codex/opencode adapters): kill() SIGTERMs only the direct CLI child. Spawns are not detached, so there is no kill(-pid) group teardown. Compare electron/devservers.js:149-175, which does group-kill correctly.

Impact

Stopping a run whose agent spawned subprocesses via its shell tool (test watchers, servers started outside the devserver feature) orphans them; they keep burning CPU and holding the worktree cwd. May be partly intentional (the devservers feature surfaces such processes), but the orphan-on-stop behavior deserves an explicit decision.

Fix direction

Spawn agent CLIs in their own process group and group-kill on stop, with an allowlist for processes the devservers feature claims.

Activity

  1. added
    bug
    plan:todo
    plan:doing
    and removed
    plan:todo
    on Aug 16, 2026
  2. currentbits commented on Aug 17, 2026

    @currentbits
    OwnerAuthor

    Fixed on coder/stopping-a-run-leaves-agent-grandchildre-0e1dba.

    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, escalate to SIGKILL on an unref'd timer — the shape devservers.js:149-175 already used. All five agent-CLI spawns (agent.js, claude.js, codex.js, kimi.js, opencode.js) now pass detached: true, and the five identical 14-line kill() bodies collapse to one call each. runner.js's app-quit reaper group-kills too, and now escalates to SIGKILL where it previously sent a bare SIGTERM.

    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.

    No allowlist — decided against, with evidence. Devservers are spawned in the Electron main process (ipc.js:713 → devservers.js:194), already detached and already their own process group. They are never descendants of an agent CLI, so an agent group-kill cannot reach them; no MCP or bridge path makes one a descendant either. A dev server the agent starts itself in its shell is in the agent's group and now dies on stop — which is the behaviour this issue asked for. An agent's nohup'd process still survives, as runner.js:316 documents deliberately.

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

    Verification. 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; moving the handler to SIGHUP makes it 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 servers cannot bind, so they are pre-existing.

    Not exercised: an end-to-end Ctrl-C of npm run dev with a live GUI run. The signal handler is unit-tested, but the real Electron path was not driven.

  3. added
    plan:done
    and removed
    plan:doing
    on Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugplan:done

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions