Skip to content

🐛 fix(mcp): detect parent death via ppid poll, bound child cleanup - #12

Merged
SWSAmor merged 2 commits into
mainfrom
fix/parent-exit-orphan-cleanup
Jul 24, 2026
Merged

SWSAmor merged 2 commits into
mainfrom
fix/parent-exit-orphan-cleanup

Conversation

@SWSAmor

@SWSAmor SWSAmor commented Jul 24, 2026

Copy link
Copy Markdown
Owner

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 the
parts main is still missing are brought over here.

Why

stdin EOF is main'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 a
    process whose parent dies is reparented to PID 1, so a change in
    process.ppid is a reliable, pipe-independent "parent gone" signal. Wired in
    start(), stopped in shutdown(); no-op on Windows / when ppid <= 1.
  • SIGHUP handled as a graceful shutdown (session-leader death).
  • 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 the
    kill sequence.
  • Config knobs (clamped, never throw on the shutdown path):
    CODE_EXECUTOR_PARENT_POLL_INTERVAL_MS,
    CODE_EXECUTOR_CHILD_SHUTDOWN_TIMEOUT_MS,
    CODE_EXECUTOR_CLIENT_CLOSE_TIMEOUT_MS.

Verification

  • typecheck green; new unit tests pass: parent-watcher (6), process-killer
    (4), shutdown-config (16).
  • Full suite (sandbox-free) shows no regression vs main (only the pre-existing
    filesystem.test.ts env failure remains).
  • Rebuilt bin/code-executor-mcp; echo -n "" | ./bin/code-executor-mcp exits 0.
  • tests/integration/orphan-cleanup.test.ts passes 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).

SWSAmor and others added 2 commits July 24, 2026 10:30
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>
@SWSAmor
SWSAmor merged commit 95e8c80 into main Jul 24, 2026
1 of 2 checks passed
@SWSAmor
SWSAmor deleted the fix/parent-exit-orphan-cleanup branch July 24, 2026 08: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