Skip to content

Commit 933ae3e

Browse files
fix(engine): a failed drawElement worker encode takes the fresh-session recovery (heygen-com#6470)
* fix(engine): a failed drawElement worker encode takes the fresh-session recovery The encode result is awaited after its frame was produced. Its failure reached the stage as a plain error, so the render ended. It now surfaces as a drawElement capture failure, like a failed produce. * fix(engine): drop the comments from the encode failure wrapper
1 parent 25b7cbd commit 933ae3e

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ export class DrawElementCaptureError extends Error {
1414
}
1515
}
1616

17+
function markRejectionHandled(promise: Promise<unknown>): void {
18+
promise.catch(() => {});
19+
}
20+
21+
export function asDrawElementCaptureFailure(
22+
frameIndex: number,
23+
encodeResult: Promise<Buffer>,
24+
): Promise<Buffer> {
25+
const wrapped = encodeResult.catch((cause: unknown) => {
26+
if (isDrawElementCaptureError(cause)) throw cause;
27+
const reason = cause instanceof Error ? cause.message : String(cause);
28+
throw new DrawElementCaptureError(frameIndex, reason, cause);
29+
});
30+
markRejectionHandled(wrapped);
31+
return wrapped;
32+
}
33+
1734
/** Structural matching survives engine/producer copies and worker error wrappers. */
1835
export function isDrawElementCaptureError(error: unknown): boolean {
1936
let current = error;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,37 @@ describe("fresh screenshot fallback", () => {
103103
});
104104
}
105105

106+
// A 21-minute render died here: the worker's encode timeout reached the stage's drain as a plain Error, so
107+
// the whole-render fresh-session recovery that every other drawElement failure gets never ran.
108+
describe("a worker encode that fails after its frame was produced", () => {
109+
const timedOut = (frame: number) =>
110+
new Error(`drawElement worker encode timed out (frame ${frame})`);
111+
112+
it("rejects a pipelined frame as a drawElement capture failure naming the frame", async () => {
113+
vi.mocked(produceDrawElementFrame).mockImplementationOnce(async () => ({
114+
encodeResult: Promise.reject(timedOut(21847)),
115+
}));
116+
const { encodeResult } = await captureFrameToBufferPipelined(await session(), 7, 7 / 30);
117+
const error = await encodeResult.catch((e: unknown) => e);
118+
expect(error).toBeInstanceOf(DrawElementCaptureError);
119+
expect(isDrawElementCaptureError(error)).toBe(true);
120+
expect((error as DrawElementCaptureError).frameIndex).toBe(7);
121+
expect((error as Error).message).toContain("worker encode timed out (frame 21847)");
122+
});
123+
124+
it("rejects every frame of a batch the same way and keeps the frames that encoded", async () => {
125+
vi.mocked(produceDrawElementFrameBatch).mockImplementationOnce(async () => ({
126+
encodeResults: [Promise.resolve(Buffer.from("ok")), Promise.reject(timedOut(8))],
127+
failedAt: null,
128+
}));
129+
const results = await captureFramesBatchPipelined(await session(), [7, 8], [7 / 30, 8 / 30]);
130+
expect((await results[0]!.encodeResult).toString()).toBe("ok");
131+
const error = await results[1]!.encodeResult.catch((e: unknown) => e);
132+
expect(isDrawElementCaptureError(error)).toBe(true);
133+
expect((error as DrawElementCaptureError).frameIndex).toBe(8);
134+
});
135+
});
136+
106137
// The recoverable-error wrapper used to throw before the diagnostics call,
107138
// so exactly the NCPR/canvas failures worth debugging produced no bundle.
108139
it("still writes per-frame diagnostics when a recoverable pipelined failure aborts the attempt", async () => {

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
} from "@hyperframes/core";
3434

3535
import { holdRenderFrameVideos } from "./renderFrameVideoHold.js";
36-
import { DrawElementCaptureError } from "./drawElementCaptureError.js";
36+
import { asDrawElementCaptureFailure, DrawElementCaptureError } from "./drawElementCaptureError.js";
3737
import { encodePng } from "../utils/alphaBlit.js";
3838
import {
3939
DEFAULT_SAMPLES_PER_FRAME,
@@ -4973,13 +4973,14 @@ export async function captureFrameToBufferPipelined(
49734973
// syncToPaintEvent = true); see initDrawElementOrTransparentBackground. The
49744974
// BeginFrame branch present in the synchronous captureFrameCore is therefore
49754975
// unreachable here and intentionally omitted.
4976-
const { encodeResult } = await produceDrawElementFrame(
4976+
const produced = await produceDrawElementFrame(
49774977
page,
49784978
options.width,
49794979
options.height,
49804980
options.quality ?? 80,
49814981
true,
49824982
);
4983+
const encodeResult = asDrawElementCaptureFailure(frameIndex, produced.encodeResult);
49834984

49844985
const captureTimeMs = Date.now() - startTime;
49854986

@@ -5124,7 +5125,10 @@ export async function captureFramesBatchPipelined(
51245125
const frameIndex = frameIndices[i];
51255126
const encodeResult = encodeResults[i];
51265127
if (frameIndex === undefined || !encodeResult) break;
5127-
results.push({ frameIndex, encodeResult });
5128+
results.push({
5129+
frameIndex,
5130+
encodeResult: asDrawElementCaptureFailure(frameIndex, encodeResult),
5131+
});
51285132
}
51295133

51305134
if (failedAt !== null) {

0 commit comments

Comments
 (0)