fix(mcp): shut down on parent death and reliably reap downstream children#6
Open
SWSAmor wants to merge 1 commit into
Open
fix(mcp): shut down on parent death and reliably reap downstream children#6SWSAmor wants to merge 1 commit into
SWSAmor wants to merge 1 commit into
Conversation
…dren Orphaned code-executor processes and the downstream MCP children they spawned (e.g. basic-memory) survived host exit, leaving the child holding its lock. Two gaps caused this despite the existing host-disconnect fix: 1. Parent death was detected only via stdin EOF, which can fail to fire (host SIGKILLed, or a wrapper/inherited fd holds the stdin pipe's write end open). Add an active process.ppid poll (parent-watcher) that triggers graceful shutdown when the process is reparented to init on POSIX, plus a SIGHUP handler. 2. disconnect() awaited client.close() with no timeout BEFORE killing children, so a wedged child stalled cleanup before the kill ran. Bound client.close() (CODE_EXECUTOR_CLIENT_CLOSE_TIMEOUT_MS) and replace the flat 2s sleep + single check with SIGTERM -> poll-for-exit -> SIGKILL after a configurable grace window (CODE_EXECUTOR_CHILD_SHUTDOWN_TIMEOUT_MS, default 30s, max 5min). Derive the overall shutdown cap from drain + grace + buffer so the graceful window is not pre-empted. Timeout getters clamp/default instead of throwing, since they are read on the shutdown path where a throw would abort the very cleanup. Tests (TDD, tests first): unit for parent-watcher, process-killer, and the config knobs; opt-in real-binary integration (orphan-cleanup) covering both the stdin-close and the isolated ppid-poll paths. Integration test skips when the binary is not built. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Orphaned
code-executorprocesses and the downstream MCP children they spawn (e.g.basic-memory) survived host exit, leaving the child running and holding its SQLite lock — to the pointbm doctoritself couldn't acquire the DB. Two gaps remained despite the existing stdin host-disconnect fix:disconnect()awaitedclient.close()with no timeout before killing children, so a wedged child (e.g.basic-memorystuck on a lock) stalled cleanup before the SIGTERM→SIGKILL loop ever ran.Changes
Active parent-liveness poll — new
src/mcp/parent-watcher.ts: pollsprocess.ppidand triggers graceful shutdown when the process is reparented to init (POSIX). Complements the stdin watcher; no-op on Windows and when launched directly by init. Wired intostart(), plus a newSIGHUPhandler.Reliable child reaping — new
src/mcp/process-killer.tskillProcessGracefully():SIGTERM → poll-for-exit → SIGKILLafter a bounded grace window. A well-behaved child (dies on SIGTERM in <1s) is not delayed; only a wedged child waits out the window.Bounded
client.close()inclient-pool.ts disconnect()so a wedged child can no longer stall the kill sequence (the root cause of surviving orphans).Overall shutdown cap is now derived (
drain + child-grace + buffer) so the graceful window isn't pre-empted by the old flat 35s race; the synchronouskillChildrenSync()exit-handler remains the final backstop.New, bounded config knobs (clamp + default rather than throw, since they're read on the shutdown path):
CODE_EXECUTOR_CHILD_SHUTDOWN_TIMEOUT_MSCODE_EXECUTOR_PARENT_POLL_INTERVAL_MSCODE_EXECUTOR_CLIENT_CLOSE_TIMEOUT_MSTests (TDD — tests written first)
parent-watcher(6),process-killer(4),shutdown-config(16) — all green.tests/integration/orphan-cleanup.test.ts, opt-in — skips when the binary isn't built):sleep, so only the parent-watcher can fire) → binary + child reaped.Runs with an isolated
$HOME, so it can never connect to real MCP servers.Verification
npm test: no regressions — the failing files are the pre-existing environment-dependent baseline (deno/python/network/scheduler/legacy imports), identical tomain. The 26 new unit tests + 2 integration tests pass.npm run typecheckclean;npm run lintclean for the touched/new files.npm run build:binarysucceeds; isolated stdin-EOF smoke test exits0.🤖 Generated with Claude Code