Claude Code agents don't clean up spawned subprocesses
Resolved 💬 3 comments Opened Feb 12, 2026 by jameschildress65 Closed Feb 15, 2026
Claude Code agents don't clean up spawned subprocesses
Summary
When Claude Code agents spawn subprocesses (e.g., pytest with parallel workers), those processes are not automatically cleaned up when the agent session ends or crashes. This results in hundreds of orphaned processes consuming system resources.
Steps to Reproduce
- Have Claude Code run tests that spawn multiple worker processes (e.g.,
pytest tests/ -q) - Agent spawns 500+ pytest worker processes
- Exit/kill the terminal or Claude Code session
- Observe that all spawned subprocesses remain running as orphaned processes
Expected Behavior
- Agent should track all spawned subprocesses
- On agent exit (normal or abnormal), all child processes should be automatically terminated
- Process groups should be used to ensure cleanup of entire process trees
- Timeout mechanisms should kill hung/stuck processes
Actual Behavior
- 550+ pytest processes remained running after agent session ended
- Processes became orphaned zombies consuming CPU/memory
- No automatic cleanup mechanism
- Required manual
pkillto terminate all processes - System experienced sustained high CPU usage and thermal load
Environment
- OS: macOS
- Claude Code: CLI version
- Model: Sonnet 4.5
Impact
- Severity: High - System resources exhausted
- Multiple recurrences of the same issue
- Required manual intervention each time
Suggested Fix
Implement process lifecycle management:
- Use process group management when spawning subprocesses
- Track all spawned PIDs in agent state
- Register cleanup handlers (atexit, signal handlers)
- On agent exit: terminate entire process group
- Implement watchdog for hung processes (timeout + force kill)
Additional Context
This issue occurred while an agent was running test suites. The problem recurred multiple times, requiring manual cleanup each time.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗