Skip to content

Commit ce1d96b

Browse files
committed
fix(core): only a caller's limit ends a seek wait; check and snapshot end it before their timeout
1 parent c74bb14 commit ce1d96b

6 files changed

Lines changed: 56 additions & 26 deletions

File tree

‎packages/cli/src/capture/captureCompositionFrame.test.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS } from "./captureTimeout.js";
12
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
23
import { tmpdir } from "node:os";
34
import { dirname, join } from "node:path";
@@ -111,7 +112,10 @@ describe("seekCompositionTimeline", () => {
111112
expect(waitForFunction).not.toHaveBeenCalled();
112113
expect(evaluate).toHaveBeenCalledTimes(4);
113114
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 1.25, false, false, false);
114-
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
115+
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), {
116+
videoFrames: true,
117+
timeoutMs: SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS,
118+
});
115119
expect(evaluate.mock.calls[2]?.[0]).toContain("window.setTimeout(finish, 100)");
116120
// Post-seek font settle: a seek can reveal glyphs whose unicode-range
117121
// subsets only start loading after the next layout (CJK snapshot reports).
@@ -126,7 +130,10 @@ describe("seekCompositionTimeline", () => {
126130
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 1.25, false, false, false);
127131

128132
await settleCompositionSeek(page);
129-
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
133+
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), {
134+
videoFrames: true,
135+
timeoutMs: SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS,
136+
});
130137
expect(evaluate).toHaveBeenCalledTimes(4);
131138
});
132139

@@ -245,7 +252,10 @@ describe("seekCompositionTimeline", () => {
245252

246253
expect(evaluate).toHaveBeenCalledTimes(3);
247254
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 4, true, false, false);
248-
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
255+
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), {
256+
videoFrames: true,
257+
timeoutMs: SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS,
258+
});
249259
expect(evaluate).toHaveBeenNthCalledWith(3, expect.any(Function), { ms: 500, frames: 2 });
250260

251261
const frames: (() => void)[] = [];
@@ -543,8 +553,12 @@ describe("DENSE_GEOMETRY_SEEK_OPTIONS", () => {
543553
return barrier.mock.calls;
544554
};
545555

546-
expect(await barrierCalls(DENSE_GEOMETRY_SEEK_OPTIONS)).toEqual([[{ videoFrames: false }]]);
547-
expect(await barrierCalls(AUDIT_SEEK_OPTIONS)).toEqual([[{ videoFrames: true }]]);
556+
expect(await barrierCalls(DENSE_GEOMETRY_SEEK_OPTIONS)).toEqual([
557+
[{ videoFrames: false, timeoutMs: SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS }],
558+
]);
559+
expect(await barrierCalls(AUDIT_SEEK_OPTIONS)).toEqual([
560+
[{ videoFrames: true, timeoutMs: SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS }],
561+
]);
548562
});
549563

550564
it("is genuinely geometry-only — no post-seek settle waits at the 600-sample cap", () => {

‎packages/cli/src/capture/captureCompositionFrame.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { oneProcessOverPipe } from "@hyperframes/engine";
2+
import { SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS } from "./captureTimeout.js";
23
import {
34
rethrowAfterBrowserCleanup,
45
launchManagedBrowser,
@@ -72,7 +73,7 @@ export interface SeekCompositionTimelineOptions {
7273
skipWebglDraws?: boolean;
7374
}
7475

75-
type SeekBarrierOptions = { videoFrames: boolean };
76+
type SeekBarrierOptions = { videoFrames: boolean; timeoutMs: number };
7677
type FontWaitOptions = { ms: number; frames: number };
7778

7879
type CompositionPageFunction =
@@ -558,7 +559,10 @@ export async function settleCompositionSeek(
558559
await Reflect.apply(waitForCompletion, window, [barrier]);
559560
}
560561
},
561-
{ videoFrames: options.waitForVideoFrames !== false },
562+
{
563+
videoFrames: options.waitForVideoFrames !== false,
564+
timeoutMs: SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS,
565+
},
562566
);
563567

564568
const animationFrameSettle = options.animationFrameSettle ?? "race";

‎packages/cli/src/capture/captureTimeout.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
export const PUPPETEER_DEFAULT_PROTOCOL_TIMEOUT_MS = 180_000;
2+
export const SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS =
3+
PUPPETEER_DEFAULT_PROTOCOL_TIMEOUT_MS - 10_000;
24

35
export class StageBudgetTimeoutError extends Error {
46
readonly budgetMs: number;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { CheckDeadline } from "./checkDeadline.js";
2+
import { SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS } from "../capture/captureTimeout.js";
23
import { logCheckProgress } from "./checkProgress.js";
34
import { BrowserCleanupError, closeManagedBrowser, onStopSignal } from "../browser/launch.js";
45
import { collectMotionBlurDrafts, dropMotionBlurConsoleEcho } from "./checkMotionBlur.js";
@@ -1221,18 +1222,23 @@ async function overlapGridInPage(
12211222
fallback: boolean;
12221223
skipDraws: boolean;
12231224
videoFrames: boolean;
1225+
timeoutMs: number;
12241226
},
12251227
seek: typeof seekInPage,
12261228
anchors: typeof anchorsInPage,
12271229
): Promise<OverlapGridSample[]> {
12281230
const audit = Reflect.get(window, "__hyperframesOverlapAudit");
12291231
const waitForCompletion = Reflect.get(window, "__hfWaitForSeekCompletion");
12301232
const samples: OverlapGridSample[] = [];
1233+
const deadline = performance.now() + grid.timeoutMs;
12311234
for (const time of grid.times) {
12321235
console.debug(`${grid.marker}${time}`);
12331236
seek(time, grid.fallback, false, grid.skipDraws);
12341237
if (typeof waitForCompletion === "function") {
1235-
await Reflect.apply(waitForCompletion, window, [{ videoFrames: grid.videoFrames }]);
1238+
const timeoutMs = Math.max(0, deadline - performance.now());
1239+
await Reflect.apply(waitForCompletion, window, [
1240+
{ videoFrames: grid.videoFrames, timeoutMs },
1241+
]);
12361242
}
12371243
const result: unknown =
12381244
typeof audit === "function" ? Reflect.apply(audit, window, [{ time }]) : [];
@@ -1256,6 +1262,7 @@ async function collectOverlapGrid(page: Page, times: number[]): Promise<Anchored
12561262
fallback: DENSE_GEOMETRY_SEEK_OPTIONS.fallbackToBridgeAndTimelines,
12571263
skipDraws: DENSE_GEOMETRY_SEEK_OPTIONS.skipWebglDraws,
12581264
videoFrames: DENSE_GEOMETRY_SEEK_OPTIONS.waitForVideoFrames,
1265+
timeoutMs: SEEK_SETTLES_BEFORE_PROTOCOL_TIMEOUT_MS,
12591266
};
12601267
const samples = (await page.evaluate(
12611268
`(${overlapGridInPage})(${JSON.stringify(grid)}, ${seekInPage}, ${anchorsInPage})`,

‎packages/core/src/runtime/adapters/seek-dispatch.test.ts‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
forceDispatchSeekEvent,
55
registerSeekCompletion,
66
resetSeekDispatchState,
7-
SEEK_SETTLE_CAP_MS,
87
waitForSeekCompletion,
98
type HfSeekEventDetail,
109
} from "./seek-dispatch";
@@ -224,30 +223,35 @@ describe("seek-dispatch", () => {
224223
});
225224

226225
it("holds registered while the barrier waits still end at the same deadline", async () => {
227-
let more = 0;
226+
let stopped = false;
228227
const again = (): void => {
229-
if (more++ > 1000) return;
228+
if (stopped) return;
230229
registerSeekCompletion(
231230
new Promise((resolve) => setTimeout(() => (again(), resolve(undefined)), 1)),
232231
"video-frame",
233232
);
234233
};
235234
again();
236235
const started = Date.now();
237-
await expect(waitForSeekCompletion({ timeoutMs: 50 })).rejects.toThrow(
238-
/still waiting on video-frame/,
239-
);
236+
try {
237+
await expect(waitForSeekCompletion({ timeoutMs: 50 })).rejects.toThrow(
238+
/still waiting on video-frame/,
239+
);
240+
} finally {
241+
stopped = true;
242+
}
240243
expect(Date.now() - started).toBeLessThan(1000);
241244
});
242245

243-
it("caps an unbounded wait at SEEK_SETTLE_CAP_MS by default", async () => {
246+
it("without a caller's timeoutMs a seek waits as long as its holds take", async () => {
244247
vi.useFakeTimers();
245248
try {
246-
registerSeekCompletion(new Promise(() => {}), "vfx");
249+
let settle = () => {};
250+
registerSeekCompletion(new Promise<void>((resolve) => (settle = resolve)), "vfx");
247251
const barrier = waitForSeekCompletion();
248-
const outcome = expect(barrier).rejects.toThrow(/still waiting on vfx/);
249-
await vi.advanceTimersByTimeAsync(SEEK_SETTLE_CAP_MS);
250-
await outcome;
252+
await vi.advanceTimersByTimeAsync(30 * 60_000);
253+
settle();
254+
await expect(barrier).resolves.toBeUndefined();
251255
} finally {
252256
vi.useRealTimers();
253257
}

‎packages/core/src/runtime/adapters/seek-dispatch.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ export type SeekHold =
2929
| "motion-blur"
3030
| "vfx"
3131
| "image-decode";
32-
/** A seek still unsettled after this is stuck: the barrier ends, naming what it still waits on. */
33-
export const SEEK_SETTLE_CAP_MS = 30_000;
3432
let _pendingCompletions = new Map<Promise<SeekCompletionResult>, SeekHold>();
3533
let _pendingFailure: { reason: unknown } | undefined;
3634
let _activeCompletionBarriers = 0;
@@ -129,8 +127,9 @@ export function hasPendingSeekCompletion(): boolean {
129127
/** All of `pending` settled, or null once `deadline` (epoch ms) passes first. */
130128
async function settledBy(
131129
pending: Promise<SeekCompletionResult>[],
132-
deadline: number,
130+
deadline: number | undefined,
133131
): Promise<SeekCompletionResult[] | null> {
132+
if (deadline === undefined) return Promise.all(pending);
134133
let timer: ReturnType<typeof setTimeout> | undefined;
135134
const late = new Promise<null>((resolve) => {
136135
timer = setTimeout(() => resolve(null), Math.max(0, deadline - Date.now()));
@@ -142,7 +141,7 @@ async function settledBy(
142141
}
143142
}
144143

145-
/** `videoFrames: false` skips only video-frame holds. Past `timeoutMs` it throws, naming the holds still pending. */
144+
/** `videoFrames: false` skips only video-frame holds. Past a caller's `timeoutMs` it throws, naming what is pending. */
146145
export async function waitForSeekCompletion(options?: {
147146
videoFrames?: boolean;
148147
timeoutMs?: number;
@@ -151,8 +150,8 @@ export async function waitForSeekCompletion(options?: {
151150
[..._pendingCompletions].filter(
152151
([, hold]) => options?.videoFrames !== false || hold !== "video-frame",
153152
);
154-
const timeoutMs = options?.timeoutMs ?? SEEK_SETTLE_CAP_MS;
155-
const deadline = Date.now() + timeoutMs;
153+
const timeoutMs = options?.timeoutMs;
154+
const deadline = timeoutMs === undefined ? undefined : Date.now() + timeoutMs;
156155
_activeCompletionBarriers += 1;
157156
let failed = _pendingFailure;
158157
try {
@@ -162,7 +161,7 @@ export async function waitForSeekCompletion(options?: {
162161
for (let pending = awaited(); pending.length > 0; pending = awaited()) {
163162
// Holds that keep arriving past the deadline must not outrun it.
164163
const results =
165-
Date.now() < deadline
164+
deadline === undefined || Date.now() < deadline
166165
? await settledBy(
167166
pending.map(([completion]) => completion),
168167
deadline,

0 commit comments

Comments
 (0)