diff --git a/packages/studio-server/src/helpers/subCompositionBuild.exit.test.ts b/packages/studio-server/src/helpers/subCompositionBuild.exit.test.ts
new file mode 100644
index 0000000000..c0a0625898
--- /dev/null
+++ b/packages/studio-server/src/helpers/subCompositionBuild.exit.test.ts
@@ -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("
Hi
", ms);
+const slow = (ms) =>
+ page("" + "".repeat(60_000) + "", 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, 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);
diff --git a/packages/studio-server/src/helpers/subCompositionBuild.ts b/packages/studio-server/src/helpers/subCompositionBuild.ts
index 4ec0081576..6920272ec8 100644
--- a/packages/studio-server/src/helpers/subCompositionBuild.ts
+++ b/packages/studio-server/src/helpers/subCompositionBuild.ts
@@ -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(
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 });
});
}