Bash tool leaves orphaned background processes running after session crash/force-quit
Summary
When a Claude Code session runs a Bash tool command that spawns background processes (cmd &), and the session is then terminated abnormally (force-quit, crash, terminal closed, sleep/wake issue) before the shell command completes, those background children are not cleaned up. They get reparented to launchd/PID 1 and keep running indefinitely with no tracking mechanism left to kill them.
Repro
- Have Claude run a Bash command that backgrounds long-running/infinite work, e.g.:
````
for i in $(seq 1 10); do (while :; do :; done) & done; <foreground command>; kill $(jobs -p)
- Kill the Claude Code process (or the parent shell) before
<foreground command>finishes. - Observe: the
while :; do :; donebackground jobs survive as orphans (PPID 1), consuming CPU indefinitely.
Impact
In my case, 10 orphaned shells each spawning CPU-burner loops ran for over a day undetected, pinning CPU and spinning fans, with nothing in Claude Code surfacing that anything was still running.
Suggested fix
When Claude Code's Bash tool spawns a shell, launch it in its own process group (e.g. setsid) and, on tool timeout/session termination/crash, kill the whole process group (negative PID) rather than just the immediate shell PID — so backgrounded children die with their parent instead of being orphaned.
Environment
- Platform: macOS (darwin)
- Claude Code version: 2.1.278 (also reproduced with orphans from 2.1.280 sessions)
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗