Skip to content

Commit 37f5f27

Browse files
fix(cli): check finishes on long projects with many video clips (#6270)
* fix(cli): check finishes on long projects with many video clips check loaded every video in the project at once. On a 500 s project with 137 clips of one 1440p source a single renderer reached 6.4 GB, the second sampling Chrome crashed, and a crashed puppeteer page never settles its calls, so the run sat until the 120 s deadline. - Load the check page in Studio's capture mode so only media near the playhead loads (peak renderer memory 15.75 GB -> 1.77 GB). - Read audio durations for the clip-fit audit from a detached element when the page has not loaded the one in the composition. - Settle every driver call with the page's crash or close, so a dead helper hands its samples back and a dead main page fails at once. * fix(cli): shorten a comment to the line limit
1 parent 1776b42 commit 37f5f27

4 files changed

Lines changed: 137 additions & 15 deletions

File tree

‎packages/cli/src/commands/validate.media-fit.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// @vitest-environment happy-dom
22
import { analyzeClipMediaFit } from "@hyperframes/engine";
33
import type { Page } from "puppeteer-core";
4-
import { afterEach, describe, expect, it } from "vitest";
4+
import { afterEach, describe, expect, it, vi } from "vitest";
55
import { auditClipDurations } from "./validate.js";
66

77
afterEach(() => {
88
document.body.innerHTML = "";
9+
vi.unstubAllGlobals();
910
});
1011

1112
async function auditAudio(attributes: string, sourceDuration: number) {
@@ -75,3 +76,27 @@ describe("audio slot fit at the authored playback speed", () => {
7576
expect(warnings[0]?.text).toContain(`Set data-duration to ~${expected}s`);
7677
});
7778
});
79+
80+
describe("audio the page never loaded", () => {
81+
it("reads its duration from a detached element on the same source", async () => {
82+
class Probe extends EventTarget {
83+
preload = "";
84+
duration = 3;
85+
set src(_url: string) {
86+
queueMicrotask(() => this.dispatchEvent(new Event("loadedmetadata")));
87+
}
88+
}
89+
vi.stubGlobal("Audio", Probe);
90+
document.body.innerHTML = '<audio id="voice" src="voice.mp3" data-duration="5"></audio>';
91+
const evaluate: Page["evaluate"] = async (fn, ...args) => {
92+
if (typeof fn !== "function") throw new Error("Expected a browser function");
93+
return window.eval(`(${fn.toString()})`)(...args);
94+
};
95+
96+
const warnings = await auditClipDurations({ evaluate }, analyzeClipMediaFit, 0);
97+
98+
expect(warnings.map((warning) => warning.text)).toEqual([
99+
expect.stringContaining('Audio "voice" is 3.00s'),
100+
]);
101+
});
102+
});

‎packages/cli/src/commands/validate.ts‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,32 @@ export async function auditClipDurations(
164164
// snapshot. Elements that already have a duration resolve immediately, so
165165
// this adds no latency in the common case.
166166
const deadline = Date.now() + maxWaitMs;
167+
// A page that defers media (check's) never loads an off-screen element, so its duration is read
168+
// from a detached element on the same source instead.
169+
const reads = new Map<HTMLMediaElement, HTMLMediaElement>();
167170
await Promise.all(
168171
nodes.map((el) => {
169172
if (Number.isFinite(el.duration) && el.duration > 0) return Promise.resolve();
173+
const probe = new Audio();
174+
probe.preload = "metadata";
175+
probe.src = el.currentSrc || el.src || el.querySelector("source")?.src || "";
176+
reads.set(el, probe);
170177
return new Promise<void>((resolve) => {
171178
const cleanup = () => {
172-
el.removeEventListener("loadedmetadata", onReady);
173-
el.removeEventListener("error", onReady);
179+
for (const target of [el, probe]) {
180+
target.removeEventListener("loadedmetadata", onReady);
181+
target.removeEventListener("error", onReady);
182+
}
174183
clearTimeout(timer);
175184
};
176185
const onReady = () => {
177186
cleanup();
178187
resolve();
179188
};
180-
el.addEventListener("loadedmetadata", onReady, { once: true });
181-
el.addEventListener("error", onReady, { once: true });
189+
for (const target of [el, probe]) {
190+
target.addEventListener("loadedmetadata", onReady, { once: true });
191+
target.addEventListener("error", onReady, { once: true });
192+
}
182193
const timer = setTimeout(onReady, Math.max(0, deadline - Date.now()));
183194
});
184195
}),
@@ -205,7 +216,9 @@ export async function auditClipDurations(
205216
"data-playback-rate": el.getAttribute("data-playback-rate"),
206217
"data-automation": el.getAttribute("data-automation"),
207218
},
208-
duration: el.duration,
219+
duration: Number.isFinite(el.duration)
220+
? el.duration
221+
: (reads.get(el)?.duration ?? Number.NaN),
209222
loop: el.loop || el.getAttribute("data-loop") === "true",
210223
});
211224
}

‎packages/cli/src/utils/checkBrowser.test.ts‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,16 @@ it("carries raw browser geometry through the page driver and pipeline", async ()
213213
it("splits the samples over a second Chrome and reports what one Chrome reports", async () => {
214214
mountCanvasFixture(`<p id="copy" data-layout-name="copy">Hi</p>`);
215215
type Message = ReturnType<typeof fakeConsoleMessage>;
216-
const run = async (cores: number, helper: "works" | "crashes" | "never opens" = "works") => {
216+
const run = async (
217+
cores: number,
218+
helper: "works" | "crashes" | "hangs after crashing" | "never opens" = "works",
219+
) => {
217220
vi.mocked(os.availableParallelism).mockReturnValue(cores);
218221
let helperSeeks = 0;
219222
const pages: ReturnType<typeof fakePage>[] = [];
220223
const helperPages = new Set<unknown>();
221224
const handlers = new Map<unknown, ((message: Message) => void)[]>();
225+
const errorHandlers = new Map<unknown, ((error: Error) => void)[]>();
222226
const log = (page: unknown, text: string) =>
223227
handlers.get(page)?.forEach((handler) => handler(fakeConsoleMessage("error", text)));
224228
const browser = Object.assign(Object.create(null), {
@@ -230,6 +234,8 @@ it("splits the samples over a second Chrome and reports what one Chrome reports"
230234
const page = fakePage();
231235
page.on = vi.fn((event: string, handler: (message: Message) => void) => {
232236
if (event === "console") handlers.set(page, [...(handlers.get(page) ?? []), handler]);
237+
if (event === "error")
238+
errorHandlers.set(page, [...(errorHandlers.get(page) ?? []), handler as never]);
233239
});
234240
const opened = pages.push(page);
235241
// Only the first page's open takes prelaunched Chromes.
@@ -247,6 +253,12 @@ it("splits the samples over a second Chrome and reports what one Chrome reports"
247253
log(page, "renderer crashed");
248254
throw new Error("Target closed");
249255
}
256+
// A crashed puppeteer page emits "error" and leaves the call pending for good.
257+
if (helperPages.has(page) && helper === "hangs after crashing" && ++helperSeeks === 2) {
258+
log(page, "renderer crashed");
259+
errorHandlers.get(page)?.forEach((handler) => handler(new Error("Page crashed!")));
260+
return new Promise<never>(() => undefined);
261+
}
250262
log(page, `seeked to ${time}`);
251263
});
252264
const result = await runBrowserCheck(
@@ -280,6 +292,10 @@ it("splits the samples over a second Chrome and reports what one Chrome reports"
280292
expect(crashed.seekingPages).toBe(2);
281293
expect(loads(crashed.messages)).toHaveLength(1);
282294
expect(sampled(crashed.messages)).toEqual(sampled(serial.messages));
295+
const hung = await run(4, "hangs after crashing");
296+
expect(hung.seekingPages).toBe(2);
297+
expect(loads(hung.messages)).toHaveLength(1);
298+
expect(sampled(hung.messages)).toEqual(sampled(serial.messages));
283299
const unopened = await run(4, "never opens");
284300
expect(unopened.pages).toHaveLength(2);
285301
expect(unopened.seekingPages).toBe(1);
@@ -429,7 +445,7 @@ it("measures text on its own opaque fill without a contrast screenshot", async (
429445
expect(page.screenshot).not.toHaveBeenCalled();
430446
});
431447

432-
it("uses check --timeout for both navigation and render-ready settling", async () => {
448+
it("uses check --timeout for both navigation and render-ready settling, and loads media near the playhead", async () => {
433449
mountCanvasFixture();
434450
const page = fakePage();
435451
installSessionMock(page);
@@ -442,7 +458,9 @@ it("uses check --timeout for both navigation and render-ready settling", async (
442458
);
443459

444460
expect(openSettledCompositionPage).toHaveBeenCalledWith(
445-
"<html></html>",
461+
expect.stringMatching(
462+
/<meta name="hyperframes-studio-preview" content="capture">[\s\S]*<html>/,
463+
),
446464
"http://127.0.0.1:3000",
447465
expect.objectContaining({
448466
navigationTimeoutMs: 30_000,
@@ -451,6 +469,35 @@ it("uses check --timeout for both navigation and render-ready settling", async (
451469
);
452470
});
453471

472+
it("fails at once when the page crashes, instead of waiting out the deadline", async () => {
473+
mountCanvasFixture(`<p id="copy" data-layout-name="copy">Hi</p>`);
474+
const page = fakePage();
475+
const onError: Array<(error: Error) => void> = [];
476+
page.on = vi.fn((event: string, handler: (error: Error) => void) => {
477+
if (event === "error") onError.push(handler);
478+
});
479+
installSessionMock(page);
480+
// A crashed puppeteer page emits "error" and leaves the call pending for good.
481+
vi.mocked(seekCompositionTimeline).mockImplementation(async () => {
482+
for (const handler of onError) handler(new Error("Page crashed!"));
483+
return new Promise<never>(() => undefined);
484+
});
485+
486+
try {
487+
await expect(
488+
runBrowserCheck(
489+
PROJECT,
490+
{ ...DEFAULT_CHECK_OPTIONS, contrast: false },
491+
{ kind: "none" },
492+
runAuditGrid,
493+
),
494+
).rejects.toThrow("The page crashed: Page crashed!");
495+
} finally {
496+
// clearAllMocks keeps implementations, and a seek that never settles would hang every later test.
497+
vi.mocked(seekCompositionTimeline).mockImplementation(async () => undefined);
498+
}
499+
});
500+
454501
it("round-trips the browser script's raw contrast candidates back into finish", async () => {
455502
// The U2 regression class: Node parses prepare's candidates for reporting,
456503
// but must hand the UNTOUCHED objects back to __contrastAuditFinish — the

‎packages/cli/src/utils/checkBrowser.ts‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ import { collectTweenRecords, findTweenTails, tweenTailMessage } from "./gsapTwe
3636
import { resolveAutoProxy } from "./projectConfig.js";
3737
import { findStageProxyBox } from "./compositionViewport.js";
3838
import type { PreviewProxyBox } from "@hyperframes/core";
39+
import { injectTagsAtHeadStart } from "@hyperframes/core/compiler/html-document";
40+
import {
41+
STUDIO_PREVIEW_CAPTURE_MODE,
42+
STUDIO_PREVIEW_MARK_META,
43+
} from "@hyperframes/core/studio-preview-mark";
3944
import {
4045
decideMediaProxyEligibility,
4146
proxyVariantFor,
@@ -254,6 +259,15 @@ function proxyFailedDraft(message: string): RuntimeDraft {
254259
return { code: "media_proxy_failed", severity: "warning", message, time: 0 };
255260
}
256261

262+
/** Studio's capture mode loads only media near the playhead, so a long project's decoders cannot exhaust memory. */
263+
async function loadCheckHtml(dir: string): Promise<string> {
264+
const { bundleWithLocalizedFonts } = await import("./bundleWithLocalizedFonts.js");
265+
return injectTagsAtHeadStart(
266+
await bundleWithLocalizedFonts(dir),
267+
`<meta name="${STUDIO_PREVIEW_MARK_META}" content="${STUDIO_PREVIEW_CAPTURE_MODE}">`,
268+
);
269+
}
270+
257271
export async function runBrowserCheck(
258272
project: ProjectDir,
259273
options: CheckOptions,
@@ -262,8 +276,7 @@ export async function runBrowserCheck(
262276
deadline?: CheckDeadline,
263277
prelaunched?: PrelaunchedBrowser,
264278
): Promise<CheckBrowserResult> {
265-
const { bundleWithLocalizedFonts } = await import("./bundleWithLocalizedFonts.js");
266-
const html = await bundleWithLocalizedFonts(project.dir);
279+
const html = await loadCheckHtml(project.dir);
267280
deadline?.enter("copies");
268281
const { findings: drafts, failedPaths } = await preResolveHostileMediaProxies(
269282
project.dir,
@@ -496,8 +509,7 @@ export async function captureFindingCrops(
496509
deadline?: CheckDeadline,
497510
): Promise<string[]> {
498511
if (requests.length === 0) return [];
499-
const { bundleWithLocalizedFonts } = await import("./bundleWithLocalizedFonts.js");
500-
const html = await bundleWithLocalizedFonts(project.dir);
512+
const html = await loadCheckHtml(project.dir);
501513
const server = await serveStaticProjectHtml(
502514
project.dir,
503515
html,
@@ -778,12 +790,37 @@ function wireNetworkListeners(page: Page, drafts: RuntimeDraft[], clock: Runtime
778790
});
779791
}
780792

793+
/**
794+
* A crashed or closed page never settles the calls pending on it, so a check waiting on one runs to
795+
* its deadline; every driver call settles with the page's death instead.
796+
*/
797+
function failWhenPageDies(page: Page, driver: CheckAuditDriver): CheckAuditDriver {
798+
const dead = new Promise<never>((_, reject) => {
799+
page.on("error", (error) =>
800+
reject(new Error(`The page crashed: ${normalizeErrorMessage(error)}`)),
801+
);
802+
page.on("close", () => reject(new Error("The page closed before the check finished")));
803+
});
804+
dead.catch(() => undefined);
805+
return Object.fromEntries(
806+
Object.entries(driver).map(([name, member]) => [
807+
name,
808+
typeof member === "function"
809+
? (...args: unknown[]) => {
810+
const result: unknown = Reflect.apply(member, driver, args);
811+
return result instanceof Promise ? Promise.race([result, dead]) : result;
812+
}
813+
: member,
814+
]),
815+
) as unknown as CheckAuditDriver;
816+
}
817+
781818
export function createPageDriver(
782819
page: Page,
783820
clock: RuntimeClock = createRuntimeClock(),
784821
sampleLog?: SampleWindow,
785822
): CheckAuditDriver {
786-
return {
823+
return failWhenPageDies(page, {
787824
initialize: (contrast) => injectAuditScripts(page, contrast),
788825
markSamples: (phase) => {
789826
if (!sampleLog) return;
@@ -839,7 +876,7 @@ export function createPageDriver(
839876
collectContrast: (time, annotations) => collectContrast(page, time, annotations),
840877
collectCraftFrame: (time) => collectCraftFrame(page, time),
841878
collectCraftFilm: () => collectCraftFilm(page),
842-
};
879+
});
843880
}
844881

845882
async function collectCraftFrame(page: Page, time: number): Promise<CraftFrame> {

0 commit comments

Comments
 (0)