Repository navigation
🐛 fix(mcp): detect parent death via ppid poll, bound child cleanup - #12
Merged
Merged
Conversation
stdin EOF (stdin-watcher) is the primary host-gone signal but is not always delivered — the host can be SIGKILLed, or a wrapper/child can hold our stdin pipe open, so EOF never arrives and the server plus every downstream MCP child it spawned orphan forever. - parent-watcher: poll process.ppid; on POSIX, reparent-to-PID-1 is a reliable parent-gone signal independent of the pipe. Belt-and-suspenders to the stdin watcher, wired in start() and stopped in shutdown(). - Handle SIGHUP (session leader gone) as a graceful shutdown. - process-killer: replace disconnect()'s flat 2s sleep with SIGTERM -> poll for exit -> SIGKILL after a bounded grace window; bound client.close()/transport.close() so a wedged child cannot stall the kill sequence. - New clamped env knobs: CODE_EXECUTOR_PARENT_POLL_INTERVAL_MS, CODE_EXECUTOR_CHILD_SHUTDOWN_TIMEOUT_MS, CODE_EXECUTOR_CLIENT_CLOSE_TIMEOUT_MS. Salvaged from the superseded #6, rebased onto current main; the stdin watcher + killChildrenSync it also carried had already landed separately. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add a section on the ppid-poll parent watcher, SIGHUP handling, and bounded graceful child cleanup to the pool-startup-resilience note, plus the three new CODE_EXECUTOR_*_MS env knobs and the salvaged test files. 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.
What
Salvages the genuinely-additive robustness parts of the superseded #6, rebased
onto the current
main. The orphan-cleanup mechanisms #6 also carried(
stdin-watcher+killChildrenSync) already landed separately, so only theparts
mainis still missing are brought over here.Why
stdinEOF ismain's only host-gone signal, but it is not always delivered —the host can be SIGKILLed, or a launcher/wrapper (or a child that inherited fd 0)
can hold our stdin pipe open, so EOF never arrives. When that happens the server
and every downstream MCP child it spawned (e.g. basic-memory holding a SQLite
lock) orphan forever.
Changes
src/mcp/parent-watcher.ts— active parent-liveness poll: on POSIX aprocess whose parent dies is reparented to PID 1, so a change in
process.ppidis a reliable, pipe-independent "parent gone" signal. Wired instart(), stopped inshutdown(); no-op on Windows / whenppid <= 1.src/mcp/process-killer.ts—disconnect()now SIGTERM → polls for exit →SIGKILLs only after a configurable grace window (was a flat 2 s sleep);
client.close()/transport.close()bounded so a wedged child can't stall thekill sequence.
CODE_EXECUTOR_PARENT_POLL_INTERVAL_MS,CODE_EXECUTOR_CHILD_SHUTDOWN_TIMEOUT_MS,CODE_EXECUTOR_CLIENT_CLOSE_TIMEOUT_MS.Verification
typecheckgreen; new unit tests pass:parent-watcher(6),process-killer(4),
shutdown-config(16).main(only the pre-existingfilesystem.test.tsenv failure remains).bin/code-executor-mcp;echo -n "" | ./bin/code-executor-mcpexits 0.tests/integration/orphan-cleanup.test.tspasses against the fresh binary:both the stdin-close path and the new ppid parent-death path reap the
downstream child and exit cleanly.
Supersedes
Closes #6 (that PR's remaining unique value is captured here, rebased; the rest
had already landed on
main).