Skip to content
Source
Sign inSign up
pingdotgg/t3code

On macOS, a process that an agent CLI starts in its own process group outlives the CLI #34459

Closedyaacovcorcos(GitHub profile, opens in a new tab) opened this issue 0 comments

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

With current main (7e565c510d), on macOS, through spawnClaudeProcessGroup with a stand-in CLI that starts a detached child and exits 1.5 s later:

// stand-in CLI
const c = require("node:child_process").spawn("/bin/sleep", ["600"], { detached: true, stdio: "ignore" });
setTimeout(() => process.exit(0), 1500);

Observed (ps -o pid=,ppid=,pgid=,command=):

fake CLI pid 69316 (leads its own process group 69316)
while the CLI runs:      69407 69316 69407 /bin/sleep 600
3 s after the CLI exit:  69407     1 69407 /bin/sleep 600

Expected behavior

What the CLI started stops when the CLI's session ends, as #15241 and #20717 made happen on Linux through the agent's systemd scope.

Actual behavior

The child keeps running, re-parented to PID 1. spawnClaudeProcessGroup kills the CLI's process group when the CLI exits (and the fd-3 watcher does the same when the server dies), which does not reach a descendant in another group, and once the CLI has exited nothing links the descendant to it. On macOS AgentScope is the no-op default, so nothing else stops it. Claude Code's bundled code spawns some processes with detached: true (its bg-pty-host helper, a persistent /bin/bash, a shell provider); whether they outlive the CLI in practice depends on the CLI's own cleanup, which I have not measured. The reproduction above uses a stand-in so the mechanism is deterministic.

A foreground command run in the CLI's own group is cleaned up (verified on #24222); this is the remaining macOS case for descendants in other groups. On Linux the systemd scope covers it when a user manager is available.

Impact

Minor bug or occasional failure

Version or commit

main at 7e565c510d

Environment

macOS 26, Node 26.12.1.

Logs or stack traces

See the reproduction above.

A possible approach, offered as a proposal, not a complete containment:

  • When the server dies, the fd-3 watcher runs while the CLI is still alive, so before its group kill it can list the CLI's live descendants in other process groups (ps -A -o pid=,ppid=,pgid=, walked from the CLI) and signal them.
  • When the CLI exits while the server runs, the links are already gone, so the server would sample the CLI's tree every few seconds and afterwards stop recorded processes that are still the same process (PID, start time, command) and what they started since. Sampling misses a child that double-forks between samples, and it cannot run if the server is killed. The watcher covers that case only for descendants still linked to the CLI; one already re-parented before the server died is out of reach of both.
  1. tanstack-source added labelsbug

  2. juliusmarminge closed this

Sign in to join this conversation.