From 5d346ceb07f485b2dedf7a3696dac85e4b13ade4 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 4 Sep 2026 12:52:54 +0800 Subject: [PATCH] fix: skip dead-pid proxy-origin records in MCP shell origin resolution (#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. --- src/mcp.ts | 27 +++++++++++++++++++++++++-- tests/instance-governance.test.ts | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/mcp.ts b/src/mcp.ts index 9208580d8e..a0106e26b1 100644 --- a/src/mcp.ts +++ b/src/mcp.ts @@ -9,7 +9,7 @@ import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; -import { readProxyInstanceFile } from "./instance.js"; +import { isPidAlive, isProxyInstanceFile, readProxyInstanceFile, type ProxyInstanceFile } from "./instance.js"; const VERSION = (() => { try { @@ -34,11 +34,34 @@ type McpToolDef = { // request id — binding is headless (next NEW session). const DEFAULT_PROXY_ORIGIN = "http://127.0.0.1:8787"; +let staleOriginNoted = false; + +function noteStaleOrigin(rec: ProxyInstanceFile): void { + if (staleOriginNoted) return; + staleOriginNoted = true; + try { + process.stderr.write( + `[bili-mcp] proxy-origin record is stale (instance ${rec.instanceId.slice(0, 8)} pid ${rec.pid} not running) — falling back to ${DEFAULT_PROXY_ORIGIN}; if your proxy runs elsewhere, set BILI_MCP_PROXY (#405)\n`, + ); + } catch {} +} + export function resolveProxyOrigin(): string { const fromEnv = process.env.BILI_MCP_PROXY?.trim(); if (fromEnv && fromEnv.length > 0) return fromEnv; const discovered = readProxyInstanceFile(); - if (discovered && /^https?:\/\/\S+$/.test(discovered.origin)) return discovered.origin; + if (discovered && /^https?:\/\/\S+$/.test(discovered.origin)) { + // #405: a JSON record left behind by a crashed/killed proxy names a + // port nothing listens on. pid liveness is the cheap check (no HTTP + // probe on the hot path); a dead record falls back to the default + // origin — where a replacement proxy binds. pid 0 means the record + // makes no liveness claim (foreign writer) — keep the legacy trust. + if (isProxyInstanceFile(discovered) && discovered.pid > 0 && !isPidAlive(discovered.pid)) { + noteStaleOrigin(discovered); + return DEFAULT_PROXY_ORIGIN; + } + return discovered.origin; + } return DEFAULT_PROXY_ORIGIN; } diff --git a/tests/instance-governance.test.ts b/tests/instance-governance.test.ts index 4f03a734b2..d04d2675cb 100644 --- a/tests/instance-governance.test.ts +++ b/tests/instance-governance.test.ts @@ -110,6 +110,24 @@ test("resolveProxyOrigin: env wins, JSON file parsed, default fallback", () => { } }); +test("resolveProxyOrigin: dead-pid JSON record falls back to default, live pid honored", () => { + const st = tmpStateDir(); + const prevEnv = process.env.BILI_MCP_PROXY; + try { + delete process.env.BILI_MCP_PROXY; + atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: deadPid() })); + assert.equal(resolveProxyOrigin(), "http://127.0.0.1:8787"); + atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: process.pid })); + assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242"); + atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: 0 })); + assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242"); + } finally { + if (prevEnv === undefined) delete process.env.BILI_MCP_PROXY; + else process.env.BILI_MCP_PROXY = prevEnv; + st.restore(); + } +}); + test("isPidAlive: self alive, dead pid not", () => { assert.equal(isPidAlive(process.pid), true); assert.equal(isPidAlive(deadPid()), false);