fix(mcp): reap MCP child processes on process exit - #2059
Open
YOMXXX wants to merge 2 commits into
Open
Conversation
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.
Summary
Fixes #1892. MCP child processes were orphaned when MiMoCode exited/restarted (reparented to PID 1, ~2 procs/~81MB leaked per restart; 22 shell+MCP pairs = 1.7GB observed).
Root cause:
packages/opencode/src/mcp/index.tsalready kills MCP server children + descendants in anaddFinalizer(on Effect teardown). But the app's exit path does NOT dispose the instance — the TUI'sonExitdoesn't callinstance.dispose(), and crash/SIGTERM never runs Effect teardown. So MCP children survived the process.Fix: a module-level
mcpChildPidsregistry + process-level exit handlers:transport.pidon each local MCP connect (success path only).closeClient(disconnect/reconnect) so the exit handler can never SIGTERM a recycled PID.process.on("exit")+SIGTERMrunreapMcpChildren()— a synchronous recursivepgrep -Pkill of each tracked PID and its descendants (the graceful finalizer stays for Effect teardown).Test Plan
reapMcpChildrenterminates a registered process; a two-level tree test covers a non-leaf PID;finallycleanup prevents leakedsleepprocesses.bun test test/mcp/reap.test.ts— 3 pass (10× stable);bun typecheckclean; MCP suite 115 pass no regression.Notes
pgrep); no-op/degraded on Windows.exit(0)after reaping (masks the signal's non-zero code for supervisors); documented in code.