From 4e206c810d78f6e61eeeb07410102136450ea213 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Fri, 4 Sep 2026 13:07:11 +0800 Subject: [PATCH] fix: skip dead-pid proxy-origin records in MCP shell origin resolution (#405) --- src/mcp.ts | 19 ++++++++++++++++-- tests/instance-governance.test.ts | 32 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/mcp.ts b/src/mcp.ts index 9208580d8e..8244660ca1 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 } from "./instance.js"; const VERSION = (() => { try { @@ -33,12 +33,27 @@ type McpToolDef = { // BILI_CONVERSATION_ID (launcher-spawned hosts like codex) has no matching // request id — binding is headless (next NEW session). const DEFAULT_PROXY_ORIGIN = "http://127.0.0.1:8787"; +let warnedStaleRecord = false; 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 naming a dead writer pid points at a port nothing + // listens on — skip it for the default origin instead of dialing it + // forever. The `pid > 0` guard is deliberate: pid 0 (foreign writer) + // and legacy plain-URL pointers make no liveness claim and keep the + // old trust semantics (unlike plugin-install, which refuses them). + if (isProxyInstanceFile(discovered) && discovered.pid > 0 && !isPidAlive(discovered.pid)) { + if (!warnedStaleRecord) { + warnedStaleRecord = true; + process.stderr.write(`[bili-mcp] recorded bili proxy (${discovered.origin}, pid ${discovered.pid}) is not running — falling back to ${DEFAULT_PROXY_ORIGIN}; set BILI_MCP_PROXY if your proxy listens elsewhere\n`); + } + 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..e0d6de9ab0 100644 --- a/tests/instance-governance.test.ts +++ b/tests/instance-governance.test.ts @@ -110,6 +110,38 @@ test("resolveProxyOrigin: env wins, JSON file parsed, default fallback", () => { } }); +test("resolveProxyOrigin: dead-pid record falls back to default origin, live pid and pid 0 trusted (#405)", () => { + const st = tmpStateDir(); + const prevEnv = process.env.BILI_MCP_PROXY; + const origStderrWrite = process.stderr.write; + const stderrOut: string[] = []; + process.stderr.write = ((chunk: string | Uint8Array) => { + stderrOut.push(String(chunk)); + return true; + }) as typeof process.stderr.write; + 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", "dead-pid record skipped"); + assert.equal(resolveProxyOrigin(), "http://127.0.0.1:8787", "re-resolves on every call"); + const notes = stderrOut.filter((l) => l.includes("is not running")); + assert.equal(notes.length, 1, "one-time stderr note"); + assert.match(notes[0], /BILI_MCP_PROXY/); + + atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242" })); + assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242", "live pid trusted"); + + atomicWriteInstanceFile(sampleInstance({ origin: "http://127.0.0.1:4242", pid: 0 })); + assert.equal(resolveProxyOrigin(), "http://127.0.0.1:4242", "pid 0 makes no liveness claim"); + } finally { + process.stderr.write = origStderrWrite; + 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);