Feature request: Process lifecycle management for spawned background tasks
Problem
When Claude Code spawns background processes (via run_in_background or long-running bash commands), these processes can become orphaned and consume significant resources without the user or Claude being aware.
Real-world example
During a session, I spawned a Python process to analyze Excel files using openpyxl. The process hung while parsing a large workbook. When the session context was compacted (due to token limits), awareness of this process was lost entirely.
Result:
- Process ran for 136 minutes
- Consumed 16GB RAM (24% of system memory)
- Used 68% CPU continuously
- Only discovered when user manually checked
ps aux
Proposed Solutions
1. Process Registry
Track all spawned child processes with:
- PID
- Start time
- Command/description
- Resource usage (periodic sampling)
2. Auto-cleanup on Context Compaction
When a session's context is compacted due to token limits:
- Include active background process PIDs in the compaction summary
- Optionally terminate long-running processes
- Or at minimum, flag them for the resumed session to check
3. Resource Watchdog
Implement configurable thresholds that trigger alerts or auto-termination:
- Maximum runtime (e.g., 30 minutes default)
- Maximum memory usage (e.g., 2GB default)
- User-configurable via settings
4. Default Timeouts for Background Tasks
The run_in_background parameter should support (or enforce) a maximum runtime:
- Default timeout (e.g., 10 minutes)
- Explicit timeout parameter for longer tasks
- Warning when spawning potentially long-running processes
5. Session Resume Process Check
When resuming from a compacted session:
- Automatically check for orphaned processes from the previous session
- Report them to the user
- Offer to terminate them
Environment
- Claude Code version: 2.1.11
- OS: macOS (Darwin 25.2.0)
- The orphaned process was spawned via Bash tool with a Python script
Additional Context
The current /tasks command helps track background tasks, but:
- It doesn't survive context compaction
- It doesn't show resource usage
- It doesn't warn about runaway processes
This is particularly important for users running Claude Code for extended sessions or on resource-constrained machines.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗