fix(mcp): reap serve --mcp child when parent is SIGKILL'd (#277)#286
Open
evanclan wants to merge 1 commit into
Open
fix(mcp): reap serve --mcp child when parent is SIGKILL'd (#277)#286evanclan wants to merge 1 commit into
evanclan wants to merge 1 commit into
Conversation
…ry#277) On Linux the kernel doesn't propagate parent death to children, and the existing `stdin.on('end' | 'close')` handlers don't always fire when an MCP host (Claude Code, opencode, …) is force-killed by the OOM killer, a `kill -9`, or a container teardown. The reporter in colbymchenry#277 ended up with three orphan `codegraph serve --mcp` processes pinned across sessions, each holding its own inotify watch set (~440k watches), which then tripped colbymchenry#276's watch-budget exhaustion in unrelated tools (Next.js, IDEs). Capture `process.ppid` once at server construction, poll it on a `setInterval`, and shut down the moment it diverges from that baseline. The interval is `.unref()`'d so it never holds the event loop open on its own; the poll period is `CODEGRAPH_PPID_POLL_MS` (default 5000ms, `0` disables for embedded hosts that re-parent on purpose). The regression test stands up a four-tier process tree (vitest → wrapper → {stdin-holder, codegraph}) so the wrapper's SIGKILL doesn't transitively close codegraph's stdin (sibling stdin-holder keeps the pipe's write-end alive). That isolates the watchdog from the pre-existing stdin-close path: the test fails without the watchdog and passes with it.
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
Adds a
process.ppidwatchdog toMCPServer.start()so acodegraph serve --mcpchild terminates when its MCP host is force-killed. Resolves #277.Problem
The existing shutdown path (
src/mcp/index.ts) leans entirely on signal handlers and stdin close events:```75:79:src/mcp/index.ts
process.on('SIGINT', () => this.stop());
process.on('SIGTERM', () => this.stop());
```
On Linux that's not enough when the host (Claude Code, opencode, …) is SIGKILL'd by the OOM killer / a `kill -9` / a container teardown:
Solution
Capture `process.ppid` once at construction, then `setInterval` (default 5s, `.unref()`'d) to check it. The moment it diverges from the baseline, we know the original parent has died and we tear down cleanly:
```text
[CodeGraph MCP] Parent process exited (ppid 9177 -> 1); shutting down.
```
Cross-platform: reparenting changes `process.ppid` on Linux and macOS; on Windows the value drops to 0 once the parent is gone, which also trips the check.
Knobs:
`stop()` is now guarded by an idempotency flag so the watchdog can't race the existing stdin-close handlers and double-close the SQLite handle / transport.
Out of scope
Test plan
Related
Made with Cursor