Skip to content

fix: skip dead-pid proxy-origin records in MCP shell origin resolution (#405) - #516

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-09-04_mcp-origin-liveness
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-09-04_mcp-origin-liveness

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Sep 4, 2026 •

Copy link
Copy Markdown
Owner

Split from #514 (fix 1 of 5, the only part still a gap on master — see review there). Completes the read-side (零验活) half of #403 — the piece the instance registry (#419) did not cover. (Note: #514's body cited #405, but that issue is the unrelated ACP_PASSTHROUGH observability bug; no auto-close intended.)

Problem

resolveProxyOrigin() (src/mcp.ts) resolves env → instance file → default with no liveness check. When a proxy crashes or is killed, its JSON instance record stays behind naming a port nothing listens on — spawned MCP shells then dial the dead origin forever, and every tool call fails until the host client is restarted (or a new proxy happens to bind that exact port).

Fix

Rebuilt on the src/instance.ts primitives (#394/#403/#417) rather than the HTTP-probe chain from #514:

  • A JSON instance record with a live pid (isPidAlive, kill-0 — synchronous, no network on the hot path) is trusted as today.
  • A record whose pid is dead is stale: skip to the default origin (http://127.0.0.1:8787, where a replacement proxy binds) with a one-time stderr note suggesting BILI_MCP_PROXY if the proxy runs elsewhere. Self-healing: the next tool call re-resolves, so a restarted proxy on the default port is picked up with no shell restart.
  • pid: 0 (record makes no liveness claim, foreign writer) and legacy plain-URL pointers keep the old trust semantics.

Why pid-check instead of #514's /__bili/health probe: the JSON record already carries the writer's pid, the check is synchronous (probe would have to become async + cached), and it exactly matches how the launcher attach and plugin-install already gate on the same file.

Tests

tests/instance-governance.test.ts: dead-pid JSON record → default origin; live pid → record origin; pid 0 → trusted.

Pre-flight

typecheck ✅ · full suite 1031/1033 (2 known plugin-agent env fails, unchanged baseline) · build ✅

#405, split from #514 fix 1)

A crashed/killed proxy leaves its JSON instance record behind naming a port
nothing listens on; MCP shells then dial the dead origin forever. resolveProxyOrigin
now checks pid liveness (kill-0) on JSON records before trusting them and falls
back to the default origin with a one-time stderr note. pid 0 (foreign record
without a liveness claim) and legacy plain-URL pointers keep the old trust.
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-04_mcp-origin-liveness (5d346ce)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-516

Each push to this PR publishes a new version under the pr-516 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr516.tgz
npm install -g package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 ework agent · qwen3.8-27b

[bot] 🏷 Done — PR: #522

Triage verdict

  • Duplicate screening: no duplicate PRs/issues. This is the rescoped split of closed fix: break the proxy-origin hijack chain (#405) #514 (fix 1 of 5) per your review there; fix 5 lives in sibling fix: reject stale in-memory snapshots that would roll back persisted session state (#405) #517. No overlap found.
  • Verified the gap on master: resolveProxyOrigin() in src/mcp.ts resolves env → instance file → default with no liveness check, while both sibling readers gate on the same file's pid — proxyOriginForInstall() in src/plugin-install.ts throws when isPidAlive(inst.pid) fails, and the launcher attach path does the same. So a dead proxy leaves a JSON record naming a dead port and spawned MCP shells dial it forever until the host restarts. Gap confirmed real, not a symptom-level rephrase of something else.
  • Design: implemented exactly as scoped in the OP (per your fix: break the proxy-origin hijack chain (#405) #514 review): live pid → trusted; dead pid → skip to http://127.0.0.1:8787 + one-time stderr note mentioning BILI_MCP_PROXY; pid: 0 and legacy plain-URL pointers keep old trust semantics (deliberate asymmetry vs plugin-install, which refuses them); env still wins. No HTTP probe — the record already carries the writer's pid, the check is synchronous, and it matches how launcher attach / plugin-install already gate.
  • Self-healing verified: origin is re-resolved on every call (no cache), so a restarted proxy writing a fresh record is picked up on the next tool call without a shell restart.

Pre-flight

  • typecheck ✅
  • full suite 947/948 — sole failure is the known env-dependent resolveClientCommand: codex/claude resolve to themselves test (this machine has /usr/bin/codex); identical on clean master (baseline 946/947)
  • build ✅
  • manual E2E on built dist/mcp.js against a stale record (dead pid, unrouted port): one-time note fired, and the tool-call error now names the fallback origin instead of the dead port

Notes (accepted limitations, report-only)

  • PID reuse: if the dead pid is later reused by an unrelated process, the stale record looks live again. Same exposure as the existing plugin-install/launcher gating.
  • Shells spawned with an explicit BILI_MCP_PROXY env pointing at a dead port still dial it — env is operator intent by design; recovery = repoint/reinstall.

No version bump, no lockfile change. Merge is yours — I can't merge PRs.

@ranxianglei

Copy link
Copy Markdown
Owner Author

Closing as duplicate of #522 — same fix (skip dead-pid proxy-origin records in MCP shell origin resolution), #522 is the rebased/current version (updated 2026-09-06). Tracking continues there.

@ranxianglei ranxianglei closed this Sep 7, 2026
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