Task tool subagent processes not terminating after completion (memory leak)
Resolved 💬 3 comments Opened Jan 19, 2026 by Kron00 Closed Jan 22, 2026
Description
The Task tool spawns subagent processes (claude --output-format stream-json) that don't terminate after the task completes. These orphaned processes accumulate over extended sessions, each consuming ~350MB of RAM.
Steps to Reproduce
- Start a Claude Code session
- Use the Task tool to spawn subagents (e.g., explore codebase, run background tasks)
- After tasks complete, check for lingering processes:
``bash``
ps -eo pid,ppid,etimes,args | grep 'output-format stream-json' | grep -v grep
Expected Behavior
Subagent processes should terminate cleanly when:
- The task completes and returns results to the parent
- The parent session closes the communication pipe
Actual Behavior
Subagent processes remain running indefinitely in Sl (sleeping, multi-threaded) state:
66480 2513 Sl /home/kron/.local/bin/claude --output-format stream-json --verbose --input-format stream-json --model claude-sonnet-4-5 --disallowedTools Bash,Read,Write,Edit,Grep,Glob,WebFetch,WebSearch,Task,NotebookEdit,AskUserQuestion,TodoWrite --setting-sources --permission-mode default
The parent PID points to an unrelated daemon process, indicating the subagent was orphaned and re-parented by the OS.
Impact
- Memory leak: ~350MB per orphaned subagent
- After extended use with multiple Task tool invocations, several GB of RAM can be consumed
- Requires manual cleanup or cron-based workaround
Workaround
Cleanup script that kills subagents older than 5 minutes:
#!/bin/bash
MAX_AGE_SECONDS=300
mapfile -t PROCS < <(ps -eo pid,etimes,args 2>/dev/null | grep 'claude' | grep '\-\-output-format stream-json' | grep -v '\-\-dangerously-skip-permissions' | grep -v grep)
for proc in "${PROCS[@]}"; do
read -r PID ELAPSED REST <<< "$proc"
[[ "$ELAPSED" -gt "$MAX_AGE_SECONDS" ]] && kill "$PID" 2>/dev/null
done
Environment
- OS: Linux 6.8.12-18-pve
- Claude Code version: Latest (installed via npm)
- Model: claude-opus-4-5-20251101 (parent), claude-sonnet-4-5 (subagents)
Possible Cause
The Task tool may not be:
- Sending SIGTERM to the subagent when the task completes
- Properly closing stdin to signal EOF
- Waiting for the child process to exit (zombie prevention)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗