fix(studio-server): a host that built a page on the build worker can exit (#7939)

* fix(studio-server): a host that built a page on the build worker can exit

* test(studio-server): a dead build worker's exit code is left to the platform

* refactor(studio-server): every build-worker queue change sets its ref in one place
This commit is contained in:
Miguel Ángel
2026-11-16 10:50:17 +00:00
committed by GitHub
parent 8ec98be29a
commit e8f1fff5fd
2 changed files with 106 additions and 12 deletions
@@ -0,0 +1,81 @@
// @vitest-environment node
import { spawnSync } from "node:child_process";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { afterAll, expect, it } from "vitest";
const packageDir = join(dirname(fileURLToPath(import.meta.url)), "../..");
const scripts = mkdtempSync(join(tmpdir(), "hf-build-worker-exit-"));
afterAll(() => rmSync(scripts, { recursive: true, force: true }));
const build = pathToFileURL(join(packageDir, "src/helpers/subCompositionBuild.ts")).href;
let hosts = 0;
/** Runs `body` in a fresh Node host with `runOffThread` and two pages, `small` and `slow` (one that takes the worker a
* while); `made` holds the workers it started. What the host prints, and how it ended, within 20 s. */
function host(body: string) {
const script = `
import workerThreads from "node:worker_threads";
import { syncBuiltinESMExports } from "node:module";
const made = [];
const Real = workerThreads.Worker;
workerThreads.Worker = class extends Real {
constructor(...args) {
super(...args);
made.push(this);
}
};
syncBuiltinESMExports();
const { runOffThread } = await import(${JSON.stringify(build)});
const options = { capture: false, gsapBaseUrl: "", sandboxedCapture: false };
const page = (html, ms) => runOffThread(
{ kind: "finish", html, runtimeUrl: "/api/runtime.js", signature: "s", projectDir: process.cwd(),
compPath: "index.html", options },
performance.now() + ms,
"index.html",
);
const small = (ms = 60_000) => page("<html><body><p>Hi</p></body></html>", ms);
const slow = (ms) =>
page("<html><body>" + "<div><p>x</p><span>y</span></div>".repeat(60_000) + "</body></html>", ms);
${body}
`;
// A file, not -e: the worker inherits the host's flags, and --input-type would stop it from starting.
const file = join(scripts, `host-${(hosts += 1)}.mjs`);
writeFileSync(file, script);
return spawnSync(process.execPath, ["--import", "tsx", file], {
cwd: packageDir,
encoding: "utf8",
timeout: 20_000,
});
}
const ended = (child: ReturnType<typeof host>, said: string | RegExp) => {
expect(child.stdout.trim(), child.stderr).toMatch(said);
expect(child.signal).toBeNull();
expect(child.status).toBe(0);
};
// fails without the fix: the worker was unref'd before its "message" listener, which refs it again, so it stayed
// ref'd after its last job and a host that built one page never exited (a test file or CLI run hung until killed).
it("a host that built a page on the build worker exits on its own", () => {
ended(host(`console.log((await small()).includes("Hi") ? "built" : "not built");`), /^built$/);
}, 30_000);
it("a host exits once a job past its deadline is refused and the one behind it is built", () => {
const child = host(`
const [late, next] = await Promise.allSettled([slow(20), small()]);
console.log(late.status, next.status);
`);
ended(child, /^rejected fulfilled$/);
}, 30_000);
it("a host exits after its build worker dies mid-job", () => {
const child = host(`
const job = slow(60_000);
made[0].once("online", () => made[0].terminate());
console.log(await job.then(() => "built", (error) => error.message));
`);
// Its exit code is the platform's (0 on some runners, 1 on others).
ended(child, /^the build worker exited \(\d+\)$/);
}, 30_000);
@@ -60,14 +60,11 @@ let nextId = 0;
function startWorker(): Worker {
const started = new Worker(workerEntry());
started.unref();
started.on(
"message",
({ id, result, error }: { id: number; result?: unknown; error?: string }) => {
const job = pending.get(id);
const job = untrack(id);
if (!job) return;
pending.delete(id);
clearTimeout(job.timer);
if (error !== undefined) job.reject(new Error(error));
else job.resolve(result);
},
@@ -79,6 +76,25 @@ function startWorker(): Worker {
return started;
}
function holdHostWhileBusy(): void {
if (pending.size === 0) worker?.unref();
else worker?.ref();
}
function track(id: number, job: Pending): void {
pending.set(id, job);
holdHostWhileBusy();
}
function untrack(id: number): Pending | undefined {
const job = pending.get(id);
if (!job) return undefined;
pending.delete(id);
clearTimeout(job.timer);
holdHostWhileBusy();
return job;
}
/** Ends `from` if it is still the worker. A crash (`failed`) fails every job it held, since any of them may be what
* crashed it; after the running job's deadline, the rest go to a fresh worker. */
function replace(from: Worker, failed?: Error): void {
@@ -86,11 +102,7 @@ function replace(from: Worker, failed?: Error): void {
worker = null;
void from.terminate();
if (failed) {
for (const [id, job] of pending) {
pending.delete(id);
clearTimeout(job.timer);
job.reject(failed);
}
for (const id of [...pending.keys()]) untrack(id)?.reject(failed);
return;
}
if (pending.size === 0) return;
@@ -110,15 +122,16 @@ export function runOffThread<J extends SubCompositionJob>(
const timer = setTimeout(
() => {
const running = pending.keys().next().value === id;
pending.delete(id);
untrack(id);
reject(new SubCompositionDeadlineError(compPath));
if (running && worker) replace(worker);
},
Math.max(0, deadline - performance.now()),
);
timer.unref?.();
pending.set(id, { job, resolve: resolve as (result: unknown) => void, reject, timer });
(worker ??= startWorker()).postMessage({ id, job });
worker ??= startWorker();
track(id, { job, resolve: resolve as (result: unknown) => void, reject, timer });
worker.postMessage({ id, job });
});
}