From 32dc9f4352f3c0218de7a76999efe1d0dbc5bb7a Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 16 Sep 2026 14:28:33 +0800 Subject: [PATCH] fix(launcher): handle async spawn 'error' in ensureProxyRunning (#835) ensureProxyRunning registered only child.on('exit'), never 'error'. An async spawn failure (EACCES/ENOENT on a mis-resolved runtime) emits 'error', which with no listener becomes an uncaughtException that kills the host process, while the poll loop burns the full SPAWN_WAIT_MS and misreports 'proxy did not become healthy'. runClient already handled 'error' correctly; the proxy-spawn path did not. Register an 'error' handler, break the poll on it, and reject with the spawn cause ('bili: proxy spawn failed ()'). Regression test added to tests/launcher.test.ts (asserts an 'error' handler is registered + rejects 'spawn failed'). Found as finding D of #809 while hands-on testing #824; the identical change already lives in open PR #824 (which targets feat/pi-native-plugin, not master). This brings it forward to master independently so the defect is closed on the mainline. Fixes #835. --- src/launcher.ts | 13 ++++++++++++- tests/launcher.test.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/launcher.ts b/src/launcher.ts index 8ce4bac3b0..f6d2b4a512 100644 --- a/src/launcher.ts +++ b/src/launcher.ts @@ -2032,16 +2032,23 @@ export async function ensureProxyRunning( // healthy — otherwise a startup crash (bad config, missing upstream, …) // burns the whole SPAWN_WAIT_MS poll window before erroring. let childExit: { code: number | null; signal: string | null } | undefined; + // #835 (finding D of #809): an async spawn failure (EACCES/ENOENT on the + // resolved runtime) emits 'error', not 'exit'. Unhandled it becomes an + // uncaughtException that kills the host process; capture it to fail fast. + let childError: unknown; child.on?.("exit", (...rest: unknown[]) => { childExit = { code: typeof rest[0] === "number" ? rest[0] : null, signal: typeof rest[1] === "string" ? rest[1] : null, }; }); + child.on?.("error", (...rest: unknown[]) => { + childError = rest[0]; + }); const deadline = now() + SPAWN_WAIT_MS; while (now() < deadline) { - if (childExit) break; + if (childExit || childError !== undefined) break; await sleepImpl(HEALTH_POLL_INTERVAL_MS); const inst = readInstance(); if (isProxyInstanceFile(inst) && inst.launchToken === launchToken) { @@ -2060,6 +2067,10 @@ export async function ensureProxyRunning( return { origin: proxyOrigin(opts.host, port), port, child, logPath }; } } + if (childError !== undefined) { + const detail = childError instanceof Error ? childError.message : String(childError); + throw new Error(`bili: proxy spawn failed (${detail}) (log: ${logPath})`); + } if (childExit) { const detail = childExit.code !== null ? `code ${childExit.code}` diff --git a/tests/launcher.test.ts b/tests/launcher.test.ts index 972b662cd7..e7d8ab39b7 100644 --- a/tests/launcher.test.ts +++ b/tests/launcher.test.ts @@ -905,6 +905,36 @@ test("ensureProxyRunning: throws when never healthy within deadline", async () = ); }); +test("ensureProxyRunning: async spawn 'error' fails fast without killing the host (#835)", async () => { + const subscribed: string[] = []; + const child: SpawnChild = { + pid: 42460, + unref() {}, + kill() { + return true; + }, + on(event, listener) { + subscribed.push(event); + if (event === "error") setImmediate(() => listener(new Error("spawn /bad/node EACCES"))); + return undefined; + }, + }; + await assert.rejects( + ensureProxyRunning( + { host: "127.0.0.1", port: 8791, passthrough: false, debug: false }, + { + spawnImpl: () => child, + fetchImpl: async () => ({ ok: false }), + readInstanceFile: () => undefined, + sleep: () => new Promise((r) => setTimeout(r, 1)), + }, + ), + /spawn failed/, + ); + assert.ok(subscribed.includes("exit")); + assert.ok(subscribed.includes("error"), "an 'error' handler was registered on the spawned proxy child"); +}); + function recordedInstance(over: Partial = {}): InstanceFile { return { origin: "http://127.0.0.1:8787",