Skip to content

fix(mcp): shut down on host disconnect to prevent orphan process trees - #3

Merged
SWSAmor merged 1 commit into
mainfrom
fix/mcp-orphan-on-host-disconnect
Jun 5, 2026
Merged

SWSAmor merged 1 commit into
mainfrom
fix/mcp-orphan-on-host-disconnect

Conversation

@SWSAmor

@SWSAmor SWSAmor commented Jun 5, 2026

Copy link
Copy Markdown
Owner

Problem

When the MCP host (e.g. Claude Code) exited, the code-executor it spawned kept running — taking its whole downstream pool (Hermes + nested code-executor) with it. Across multiple host sessions, entire orphan process trees accumulated (observed: 12 code-executor + 6 hermes from 4 dead sessions, while only 2 hosts were live).

Root cause

On macOS a child process receives no signal when its parent dies (no PR_SET_PDEATHSIG equivalent). The only reliable signal that the host is gone is EOF on stdin (the closed pipe). But the SDK's StdioServerTransport listens only for 'data'/'error' on stdin — it never translates that EOF into a transport close. With nothing acting on the EOF, the process never reached 'exit', so even the existing process.on('exit') reap backstop never fired.

Note: a graceful host exit (host sends SIGTERM) always tore down cleanly via the existing signal handlers. The orphaning happened specifically when the host did not signal — it just closed the pipe.

Fix

Watch process.stdin for 'end'/'close' after the transport connects, and trigger the existing graceful shutdown() (which drains, disconnects the downstream pool, SIGTERM→SIGKILLs its children, then process.exit(0)).

The logic lives in a standalone, side-effect-free module (src/mcp/stdin-watcher.ts) so it is unit-testable without the server's startup side effects (signal handlers, server.start()).

Changes

  • src/mcp/stdin-watcher.ts (new) — watchClientDisconnect(stdin, onDisconnect), fires once on 'end'/'close'.
  • src/index.ts — wire it up right after server.connect(transport).
  • tests/stdin-watcher.test.ts — 4 tests (end / close / both-fire-once / data-does-not-trigger).
  • CHANGELOG.md — entry under the existing Unreleased "Fixed" block.

Verification

  • npm run typecheck — clean.
  • New tests green. Full suite: identical pass/fail vs baseline (pre-existing env failures only — sandbox EPERM, network, darwin platform config); zero regressions.
  • No new lint errors (the 6 in index.ts are pre-existing, out of scope).
  • End-to-end on the compiled bun binary: echo -n "" | timeout 10 ./bin/code-executor-mcp → stdin end — MCP client disconnected → graceful shutdown → exit 0 (not a 124 hang).

🤖 Generated with Claude Code

When the MCP host (e.g. Claude Code) exited, the spawned code-executor kept
running along with its whole downstream pool (Hermes + nested code-executor),
accumulating orphan trees across sessions. On macOS a child gets no signal when
its parent dies, and the SDK's StdioServerTransport listens only for
'data'/'error' on stdin — it never turns the EOF from the closed pipe into a
transport close. With nothing acting on the EOF the process never reached
'exit', so even the process.on('exit') reap backstop never fired.

Watch process.stdin for 'end'/'close' after the transport connects and trigger
the existing graceful shutdown() (which disconnects the downstream pool,
SIGTERM/SIGKILLs its children, then exits). Logic lives in a standalone,
side-effect-free module so it is unit-testable without the server's startup
side effects.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@SWSAmor
SWSAmor merged commit c933473 into main Jun 5, 2026
0 of 2 checks passed
@SWSAmor
SWSAmor deleted the fix/mcp-orphan-on-host-disconnect branch June 5, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant