claude -p leaves orphaned MCP child processes after exit
Bug
When claude -p (non-interactive/programmatic mode) finishes execution, the CLI node.exe process exits but all MCP server child processes remain alive as orphans. They are never cleaned up.
Reproduction
# Count node.exe processes before
powershell -Command "(Get-Process node -EA Silent).Count"
# Run a simple prompt with MCP config
claude -p "respond with OK" --mcp-config mcp-config.json --strict-mcp-config
# Count after — orphaned MCP processes remain
powershell -Command "(Get-Process node -EA Silent).Count"
The delta shows MCP servers (node.exe, python.exe) that were spawned by the CLI but never terminated.
Impact
In automated setups using Task Scheduler / cron with claude -p, each execution leaves 2-5 orphaned MCP processes (~80 MB each). With 4-6 scheduled runs per day, this accumulates to 10-30 zombie processes and ~1-2 GB of wasted RAM daily.
Environment
- Windows 11 Pro
- Claude Code 2.1.91
- MCPs configured via
--mcp-configwith--strict-mcp-config
Expected behavior
When claude -p exits, it should terminate all MCP child processes it spawned (e.g., via process tree kill or Job Object on Windows, process group kill on Linux/macOS).
Current workaround
A scheduled PowerShell script (mcp-health-check.ps1) runs every 30 minutes: finds all active Claude CLI processes, walks their descendant trees, identifies MCP-pattern processes not descended from any living CLI, and kills them.
🤖 Generated with Claude Code
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗