fix(mcp): recursive descendant PID tracking for shell-wrapper MCP servers - #40110
Closed
PINKIIILQWQ wants to merge 0 commit into
Closed
fix(mcp): recursive descendant PID tracking for shell-wrapper MCP servers#40110PINKIIILQWQ wants to merge 0 commit into
PINKIIILQWQ wants to merge 0 commit into
Conversation
13 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When MCP stdio servers are configured with a shell wrapper (e.g.
command: /bin/bash,args: ["./run-local-mcp.sh"]), the process tree looks like:Only the wrapper (direct child) is tracked by
_snapshot_child_pids(). When the wrapper exits (daemonizer pattern) or the grandchild is in a different process group, the real server process survives cleanup. Over multiple cron runs, these accumulate as zombies and block the port, causingECONNREFUSEDon subsequent connections.The existing pgid-based fix (a29d64e) handles grandchildren in the same process group, but fails when:
setsid()escape)killpgbehavior differsFixes #26042.
Changes
Core —
tools/mcp_tool.py(134 lines)_discover_descendants(pid)— new helper: DFS over/proc/{pid}/task/{pid}/children(Linux) returning ALL descendants of a PID. Falls back topsutil.Process(pid).children(recursive=True)on macOS/Windows. Handles partial/procread failures gracefully (processes that exit mid-walk)._snapshot_child_pids()— upgraded from one-level/procread to recursive BFS. psutil fallback now useschildren(recursive=True). This ensures grandchildren are captured AT SPAWN TIME, not just during cleanup._kill_orphaned_mcp_children()— two-layer reaper: existingos.killpg(pgid, sig)for same-pgroup cases + new/procdescendant walk for cross-pgroup cases. Both SIGTERM and SIGKILL phases cover grandchildren. Docstring updated.Tests —
tests/tools/test_mcp_pid_tracking.py(165 lines, new)test_recursive_linux_proc_childrentest_handles_proc_read_errors_gracefullytest_kills_children_of_orphanstest_no_proc_still_kills_tracked_pidsTests —
tests/tools/test_mcp_stability.py(291 lines, expanded)/proctree test: grandchild + great-grandchild discovered/procraisesFileNotFoundError)/proc, no psutil) → empty set_send_signaltest coverage (correct SIGTERM/SIGKILL delivery)bash -c "sleep 60", verify_snapshot_child_pids()sees it)Documentation
website/docs/user-guide/features/mcp.md: wrapper-avoidance recommendations, cross-platform PID tracking table (Linux / macOS / Windows)website/docs/developer-guide/contributing.md: cross-platform PID tracking sectionREADME.md+README.zh-CN.md: feature section for MCP grandchild PID trackingHousekeeping
.gitignore: addedstages/to prevent pipeline stage metadata leakage into the repoArchitecture
The fix uses a three-layer strategy:
_snapshot_child_pids()discovers all descendants at spawn time, so grandchildren are tracked in_stdio_pidsfrom the start._kill_orphaned_mcp_children()usesos.killpg(pgid, sig)for same-pgroup descendants (Linux only)./proc/{pid}/task/{pid}/childrento find and signal orphans directly.Layers 2+3 are applied in both SIGTERM (graceful) and SIGKILL (forced) phases.
Backward Compatibility
Fully backward compatible. Existing MCP configurations unchanged. New logic only activates during orphan detection — no impact on normal MCP tool calls:
_discover_descendants()is only called during cleanup paths_snapshot_child_pids()returns the same PIDs as before (plus newly visible grandchildren — which were always alive, just untracked)Verification
yamlmodule in venv, conftestos.killguard)bash -c "sleep 60")/proc), macOS (psutil), Windows (psutil + no killpg)