Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`
Expand Down
30 changes: 30 additions & 0 deletions tests/launcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {}): InstanceFile {
return {
origin: "http://127.0.0.1:8787",
Expand Down
Loading