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",