From 1ce69fd8181b2911ec4a22a73e150197ead00ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 21 Nov 2026 00:35:24 +0000 Subject: [PATCH] fix(producer): a compile's downloads stop when the compile returns, however it ends (#8321) * test(producer): a failed compile stops its downloads, and a file's media is capped up front * fix(producer): a compile's downloads stop when the compile returns, however it ends * test(producer): media with a set duration is capped up front too * fix(producer): reserve the clamp pass's media before it downloads any * test(producer): a probe's signal fires on the render's cancel, whatever object carries it --- .../htmlCompiler.compileAssetLifetime.test.ts | 79 +++++++++++++++++++ .../services/htmlCompiler.probeCancel.test.ts | 3 +- .../producer/src/services/htmlCompiler.ts | 70 ++++++++++------ 3 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 packages/producer/src/services/htmlCompiler.compileAssetLifetime.test.ts diff --git a/packages/producer/src/services/htmlCompiler.compileAssetLifetime.test.ts b/packages/producer/src/services/htmlCompiler.compileAssetLifetime.test.ts new file mode 100644 index 0000000000..2710194004 --- /dev/null +++ b/packages/producer/src/services/htmlCompiler.compileAssetLifetime.test.ts @@ -0,0 +1,79 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { afterEach, expect, it, vi } from "vitest"; +import { compileForRender, MAX_REMOTE_ASSETS } from "./htmlCompiler.js"; + +const requests = vi.hoisted(() => [] as { url: string; signal?: AbortSignal }[]); +vi.mock("../utils/urlDownloader.js", async (importOriginal) => ({ + ...(await importOriginal()), + // A transport that never answers: only an abort settles a request. + downloadToTemp: (url: string, _dir: string, _timeoutMs?: number, signal?: AbortSignal) => { + requests.push({ url, signal }); + return new Promise((_resolve, reject) => { + signal?.addEventListener("abort", () => reject(signal.reason), { once: true }); + }); + }, + fetchPublicHttpsText: async () => { + throw new Error("offline test"); + }, +})); + +const dirs: string[] = []; +afterEach(() => { + requests.length = 0; + for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); +}); + +const videos = (prefix: string, count: number) => + Array.from( + { length: count }, + (_, i) => + ``, + ).join(""); +const scene = (id: string, body: string) => + ``; +const mount = (id: string) => + `
`; + +function compile(files: Record) { + const dir = mkdtempSync(join(tmpdir(), "hf-asset-lifetime-")); + dirs.push(dir); + for (const [name, html] of Object.entries(files)) writeFileSync(join(dir, name), html); + return compileForRender(dir, join(dir, "index.html"), join(dir, "out"), { + allowSystemFontCapture: false, + }); +} +const root = (body: string) => + `
${body}
`; + +// fails without the fix: a failed compile left its other downloads running, with nothing to stop them. +it("stops every download of a compile that fails", async () => { + const compiled = compile({ + "index.html": root(mount("a") + mount("b")), + "a.html": scene("a", videos("a", 20)), + "b.html": scene("b", videos("b", MAX_REMOTE_ASSETS)), + }); + + await expect(compiled).rejects.toThrow(`at most ${MAX_REMOTE_ASSETS} are downloaded`); + const started = requests.length; + expect(started).toBeGreaterThan(0); + expect(requests.every((request) => request.signal?.aborted)).toBe(true); + await new Promise(setImmediate); + expect(requests).toHaveLength(started); +}); + +// fails without the fix: each source was reserved on its own, so the first ones started before the cap refused. +it.each([ + ["without", "