GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(core): clean up orphaned MCP processes after a crash by rekram1-node · Pull Request #59398 · anomalyco/opencode

Conversation

@rekram1-node

@rekram1-node rekram1-node commented Oct 26, 2026 •
edited by opencode-agent Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Problem

If the opencode server crashes or gets SIGKILLed, local MCP servers that don't exit on stdin EOF (like npx wrappers) keep running forever, reparented to PID 1. They pile up across restarts. Part of #50363.

Normal shutdown already kills the MCP process group, but nothing does that when the server dies without running its cleanup.

Change

I start each local MCP server through a small sh guard in packages/core/src/mcp/stdio.ts. The server gives the guard an extra pipe (fd 3) and never writes to it. The kernel closes that pipe however the server exits, so a watcher in the guard sees EOF and SIGKILLs its own process group.

opencode server ──fd 3 pipe──┐
                             ▼
sh guard (group leader)
├── watcher: read fd 3 → EOF → kill -KILL 0
└── MCP server (stdin/stdout passed through)
  • No state files or startup sweep, nothing shared between servers, and cleanup happens right away.
  • kill 0 hits the watcher's own group, which exists as long as the watcher does, so a reused PID can't be hit.
  • When the MCP server exits on its own, the guard stops the watcher and exits with the server's status, so normal stops work as before.
  • Only applies to servers spawned on this host on macOS and Linux. Workspace-placed servers run elsewhere, so they aren't wrapped.

Out of scope

  • Windows. There are no process groups to signal there; doing this properly needs a job object with kill-on-close, which needs native code.
  • Shell tool jobs and other process groups. The same trick would work, but wrapping every spawn changes PIDs and exit statuses for many more callers.

Verification

  • Repro: serve with a stdio MCP server that ignores stdin EOF, then kill -9 the server. Before: the MCP server stays alive with PPID 1. After: it's gone immediately.
  • Disconnect, reconnect and SIGTERM shutdown still leave nothing behind.
  • bun run script/test.ts test/mcp.test.ts and bun typecheck in packages/core, bun run check from the root.

Visual

<svg xmlns="http://www.w3.org/2000/svg" width="240" height="72" viewBox="0 0 240 72" fill="none" stroke="currentColor" stroke-width="2">
<style>
.s{animation:s 4s infinite}.l{animation:l 4s infinite}.c{animation:c 4s infinite}.x{opacity:0;animation:x 4s infinite}
@keyframes s{0%,30%{opacity:1}38%,85%{opacity:.25}95%,100%{opacity:1}}
@keyframes x{0%,30%{opacity:0}38%,85%{opacity:1}95%,100%{opacity:0}}
@keyframes l{0%,32%{opacity:.5}40%,85%{opacity:0}95%,100%{opacity:.5}}
@keyframes c{0%,42%{opacity:1;transform:none}55%,85%{opacity:0;transform:translateY(6px)}95%,100%{opacity:1;transform:none}}
@media (prefers-reduced-motion:reduce){.s,.l,.c,.x{animation:none}}
</style>
<rect class="s" x="16" y="20" width="64" height="32" rx="6"/>
<path class="s" d="M32 36h32" opacity=".6"/>
<path class="x" d="M38 26l20 20M58 26L38 46" stroke-width="3"/>
<path class="l" d="M80 36h48" stroke-dasharray="4 4"/>
<g class="c"><rect x="128" y="20" width="44" height="32" rx="6"/><rect x="184" y="20" width="44" height="32" rx="6" opacity=".6"/><path d="M172 36h12" opacity=".6"/></g>
</svg>

Added by the opencode bot (AI-generated).

Related: #50363

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added the contributor label Oct 26, 2026
opencode-agent[bot]
opencode-agent Bot reviewed Oct 26, 2026
View reviewed changes

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works. On the base, kill -9 of serve left a stdio MCP server that ignores stdin EOF, and its child, running with PPID 1. With this PR they're all gone within 3s. Disconnect, reconnect and SIGTERM shutdown still leave nothing behind. The core typecheck passes, and the MCP tests match the base: the one failure there also fails on the base.

Public API: unchanged.

Worth knowing: a nonexistent command now fails as MCP server process exited with code 127: …: not found instead of the spawner's NotFound error. It's still readable, since stderr is included.

The SIGKILL test in process-guard.test.ts fails here on every run. The guard does its job, but the test counts zombies as alive (see inline).

Related: #50363

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/util/src/process-guard.test.ts Outdated

// SIGTERM ends the descendant at once; the leader ignores it until the SIGKILL two seconds later.
const deadline = Date.now() + 10_000
while (pids.some(alive) && Date.now() < deadline) await Bun.sleep(50)

@opencode-agent opencode-agent Bot Oct 26, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails every run in our sandbox, even though the guard works. The killed leader ends up a zombie (STAT Z, PPID 1) because PID 1 there doesn't reap orphaned processes, and process.kill(pid, 0) succeeds for zombies. Containers without an init process behave the same way. Could alive treat zombies as dead (e.g. check the state in /proc/<pid>/stat, or ps -o stat=)? Then the test checks that the processes stopped, not whether PID 1 reaped them. The real 2s/10s waits also make it slow; a test with only the descendant (no TERM-ignoring leader) would avoid the 2s escalation.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
rekram1-node added 2 commits October 26, 2026 15:55
@rekram1-node
fix(core): clean up orphaned MCP processes after a crash
00a4e66
@rekram1-node
fix(core): shrink the MCP orphan guard to stdio.ts
6bcb5fe
@rekram1-node
rekram1-node force-pushed the mcp-orphans branch from 22c9e79 to 6bcb5fe Compare October 26, 2026 20:55
opencode-agent[bot]
opencode-agent Bot reviewed Oct 26, 2026
View reviewed changes

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still fixes the crash case. I reran the serve + kill -9 repro with a stdio MCP server that ignores stdin EOF. On the base it stays alive with PPID 1; with this PR it and its child are gone right away. Disconnect, reconnect and SIGTERM shutdown still leave nothing behind. The core typecheck passes, and the MCP tests match the base.

The earlier point about the flaky SIGKILL test is resolved: that test and process-guard.ts were removed, and the guard is now inline in stdio.ts.

Public API: unchanged.

No new concerns.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@rekram1-node

rekram1-node commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

Closing this in favor of a smaller approach: track the process groups the spawner starts and kill them in an exit hook, instead of wrapping every spawn in sh. I'll open that as a separate PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rekram1-node rekram1-node closed this Oct 26, 2026
@rekram1-node rekram1-node mentioned this pull request Oct 26, 2026
fix(util): kill spawned process groups when opencode exits #59411
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@opencode-agent opencode-agent[bot] opencode-agent[bot] left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

contributor

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@rekram1-node
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.