[BUG] Subagent processes not terminated after parent session ends, causing memory exhaustion
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report
- [x] I am using the latest version of Claude Code
What's Wrong?
Subagent processes spawned via the Task tool persist indefinitely after the parent Claude Code session ends. Over approximately 2 days, 43 orphaned subagent processes accumulated, consuming ~10.8 GB of RAM. This exhausted all system memory (15 GB RAM + 7.7 GB swap at 100%), causing the system to become severely unresponsive with load averages exceeding 112-173 on a 16-core machine.
The orphaned processes all had the signature:
/root/.local/bin/claude --output-format stream-json --verbose --input-format stream-json --model claude-sonnet-4-5 --resume <session-id> --disallowedTools Bash,Read,Write,Edit,Grep,Glob,WebFetch,WebSearch,Task,NotebookEdit,AskUserQuestion,TodoWrite --setting-sources --permission-mode default
Manual intervention (kill -9) was required to terminate these processes and recover the system.
What Should Happen?
Subagent processes should automatically terminate when:
- Their parent Claude Code session exits (gracefully or via interrupt)
- Their assigned task completes
- The session they belong to is closed
Error Messages/Logs
No explicit error messages. The symptom was system-wide resource exhaustion:
$ uptime
14:58:47 up 2 days, 20:32, 2 users, load average: 112.54, 173.17, 103.66
$ free -h
total used free shared buff/cache available
Mem: 15Gi 14Gi 446Mi 81Mi 731Mi 762Mi
Swap: 7.7Gi 7.7Gi 34Mi
$ pgrep -c claude
45
$ ps aux | grep '[c]laude' | awk '{sum+=$6} END {print "Total Claude RSS: " sum/1024 " MB"}'
Total Claude RSS: 10780.5 MB
Steps to Reproduce
- Use Claude Code with features that spawn subagents (Task tool with Explore, Plan, or other subagent types)
- End the session (Ctrl+C, close terminal, or
/exit) - Start new sessions and repeat over 1-2 days
- Check for orphaned processes:
ps aux | grep 'claude.*--output-format stream-json' - Observe accumulated subagent processes that should have terminated
Note: Reproduction may require multiple sessions over time. The exact trigger for orphaning is unclear - it may be related to session interruption (Ctrl+C) rather than graceful exit.
Claude Model
claude-sonnet-4-5 (subagents)
claude-opus-4-5 (main session)
Is this a regression?
Unknown - first time encountering this issue after extended use with subagents.
Claude Code Version
2.1.19 (Claude Code)
Platform
Anthropic API (Direct)
Operating System
CentOS Stream 9 (Linux 5.14.0-604.el9.x86_64)
Terminal/Shell
Bash (/bin/bash), xterm-256color
Additional Information
System specs: 16-core Intel i7-10875H, 16 GB RAM, 7.7 GB swap
Recovery: Killing orphaned processes immediately recovered ~8 GB RAM and ~6.6 GB swap:
ps aux | grep '[c]laude.*--output-format stream-json' | awk '{print $2}' | xargs -r kill -9
Potential root cause: Subagent processes may not be receiving SIGTERM/SIGHUP when parent process exits, or there's no process group management ensuring children terminate with parent.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗