Skip to content

Commit 8e457d4

Browse files
fix(engine): fail the render when an injected video frame cannot decode (#4974)
* fix(engine): fail the render when an injected video frame cannot decode * fix(engine): report BeginFrame capture errors at once, not after the stall watchdog * fix(engine): skip the diagnostic screenshot for any session launched for BeginFrame
1 parent b71aa93 commit 8e457d4

4 files changed

Lines changed: 82 additions & 5 deletions

File tree

‎packages/engine/src/services/frameCapture-freshFallback.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,28 @@ describe("fresh screenshot fallback", () => {
122122
expect(existsSync(join(s.outputDir, "diagnostics", "frame-error-7.json"))).toBe(true);
123123
});
124124

125+
// A BeginFrame page paints only when the engine sends a frame, so a diagnostic
126+
// page.screenshot never resolves there and held the real error until the stall watchdog.
127+
it.each(["beginframe", "drawelement"] as const)(
128+
"rejects a %s capture failure on a BeginFrame launch with its own error, without a page screenshot",
129+
async (captureMode) => {
130+
const s = await session();
131+
s.launchCaptureMode = "beginframe";
132+
s.captureMode = captureMode;
133+
s.onBeforeCapture = async () => {
134+
throw new Error('Video frame for "clip" failed to load (inline frame)');
135+
};
136+
const screenshot = vi.fn(() => new Promise<Buffer>(() => {}));
137+
Object.assign(s.page, { screenshot, content: async () => "<html></html>" });
138+
139+
await expect(captureFrameToBuffer(s, 7, 7 / 30)).rejects.toThrow(
140+
'Video frame for "clip" failed to load',
141+
);
142+
expect(screenshot).not.toHaveBeenCalled();
143+
expect(existsSync(join(s.outputDir, "diagnostics", "frame-error-7.json"))).toBe(true);
144+
},
145+
);
146+
125147
it("rejects a suspect tiny frame without substituting a stale screenshot", async () => {
126148
vi.mocked(captureDrawElementFrame).mockResolvedValueOnce(Buffer.alloc(100));
127149
await expect(captureFrameToBuffer(await session(), 7, 7 / 30)).rejects.toThrow(

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,10 @@ async function captureFrameErrorDiagnostics(
26382638
const diagnosticsDir = join(session.outputDir, "diagnostics");
26392639
if (!existsSync(diagnosticsDir)) mkdirSync(diagnosticsDir, { recursive: true });
26402640
const base = join(diagnosticsDir, `frame-error-${frameIndex}`);
2641-
await session.page.screenshot({ path: `${base}.png`, type: "png", fullPage: true });
2641+
const pageScreenshotCanResolve = session.launchCaptureMode !== "beginframe";
2642+
if (pageScreenshotCanResolve) {
2643+
await session.page.screenshot({ path: `${base}.png`, type: "png", fullPage: true });
2644+
}
26422645
const html = await session.page.content();
26432646
writeFileSync(`${base}.html`, html, "utf-8");
26442647
writeFileSync(

‎packages/engine/src/services/screenshotService.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,54 @@ describe("initTransparentBackground", () => {
240240
});
241241
});
242242

243+
describe("injectVideoFramesBatch load failures", () => {
244+
it("rejects naming the video when an injected frame fails to decode", async () => {
245+
const { window, document } = parseHTML(
246+
'<html><body><div id="root"><video id="clip" style="position:absolute;width:100%;height:100%"></video></div></body></html>',
247+
);
248+
Object.defineProperty(window.HTMLImageElement.prototype, "decode", {
249+
configurable: true,
250+
value: async () => {
251+
throw new Error("The source image cannot be decoded.");
252+
},
253+
});
254+
const video = document.getElementById("clip") as HTMLVideoElement;
255+
video.getBoundingClientRect = () => ({ width: 1920, height: 1080 }) as DOMRect;
256+
const computedStyle = document.createElement("div").style;
257+
computedStyle.opacity = "1";
258+
Object.defineProperty(window, "getComputedStyle", {
259+
configurable: true,
260+
value: () => computedStyle,
261+
});
262+
const globals = globalThis as unknown as { window?: typeof window; document?: Document };
263+
const previousWindow = globals.window;
264+
const previousDocument = globals.document;
265+
globals.window = window;
266+
globals.document = document;
267+
try {
268+
const page = {
269+
evaluate: async (
270+
fn: (
271+
updates: Array<{ videoId: string; dataUri: string }>,
272+
visualProperties: string[],
273+
) => Promise<void>,
274+
updates: Array<{ videoId: string; dataUri: string }>,
275+
visualProperties: string[],
276+
) => fn(updates, visualProperties),
277+
} as unknown as Page;
278+
279+
await expect(
280+
injectVideoFramesBatch(page, [{ videoId: "clip", dataUri: "data:image/jpeg;base64,AAAA" }]),
281+
).rejects.toThrow(
282+
'Video frame for "clip" failed to load (inline frame): The source image cannot be decoded.',
283+
);
284+
} finally {
285+
globals.window = previousWindow;
286+
globals.document = previousDocument;
287+
}
288+
});
289+
});
290+
243291
describe("injectVideoFramesBatch replacement layout", () => {
244292
it("does not copy opposing inset constraints onto the injected frame image", async () => {
245293
const { window, document } = parseHTML(

‎packages/engine/src/services/screenshotService.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,11 +795,15 @@ export async function injectVideoFramesBatch(
795795
img.decoding = "sync";
796796
if (img.getAttribute("src") !== item.dataUri) {
797797
img.src = item.dataUri;
798+
const source = item.dataUri.startsWith("data:") ? "inline frame" : item.dataUri;
798799
pendingDecodes.push(
799-
img
800-
.decode()
801-
.catch(() => undefined)
802-
.then(() => undefined),
800+
img.decode().catch((error: unknown) => {
801+
throw new Error(
802+
`Video frame for "${item.videoId}" failed to load (${source}): ${
803+
error instanceof Error ? error.message : String(error)
804+
}`,
805+
);
806+
}),
803807
);
804808
}
805809
img.style.opacity = String(computedOpacity);

0 commit comments

Comments
 (0)