Skip to content

Commit 65b77cf

Browse files
committed
fix(server): bound desktop snapshot metrics by the snapshot deadline
1 parent 125c173 commit 65b77cf

3 files changed

Lines changed: 79 additions & 8 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,3 +1707,62 @@ it.live.each([
17071707
}),
17081708
).pipe(Effect.provide(layer)),
17091709
);
1710+
1711+
it.live("a stalled desktop metrics read times out and resumes snapshot viewers", () =>
1712+
Effect.scoped(
1713+
Effect.gen(function* () {
1714+
desktopRendersNext = true;
1715+
const { browser, broker, tabId } = yield* ready;
1716+
yield* browser.attachViewer(viewerInput(tabId, false));
1717+
const context = desktopConnections[0]!.context;
1718+
const reading = Promise.withResolvers<void>();
1719+
let stalled = true;
1720+
for (const session of context.sessions) {
1721+
const send = session.send.getMockImplementation()!;
1722+
session.send.mockClear().mockImplementation(async (method, input) => {
1723+
if (
1724+
method === "Runtime.evaluate" &&
1725+
String((input as { expression?: string })?.expression).includes("ratio:")
1726+
) {
1727+
if (stalled) {
1728+
reading.resolve();
1729+
return new Promise(() => {});
1730+
}
1731+
return { result: { value: { ratio: 1, width: 1280, height: 800, zoom: 1 } } };
1732+
}
1733+
return send(method, input);
1734+
});
1735+
}
1736+
const snapshot = broker.invoke<PreviewAutomationSnapshot>({
1737+
scope,
1738+
tabId,
1739+
operation: "snapshot",
1740+
input: {},
1741+
});
1742+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });
1743+
try {
1744+
const pending = yield* snapshot.pipe(
1745+
Effect.exit,
1746+
Effect.tap(() => Effect.sync(() => reading.resolve())),
1747+
Effect.forkScoped,
1748+
);
1749+
yield* Effect.promise(() => reading.promise);
1750+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(9_999));
1751+
expect(pending.pollUnsafe()).toBeUndefined();
1752+
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(1));
1753+
const result = pending.pollUnsafe();
1754+
expect(result).toBeDefined();
1755+
expect(JSON.stringify(result)).toContain(
1756+
"The page was too busy to capture a snapshot in time",
1757+
);
1758+
const viewerSession = context.sessions.at(-1)!;
1759+
expect(viewerSession.send).toHaveBeenCalledWith("Page.stopScreencast");
1760+
expect(viewerSession.send).toHaveBeenCalledWith("Page.startScreencast", expect.anything());
1761+
stalled = false;
1762+
expect((yield* snapshot).screenshot).toMatchObject({ width: 1280, height: 800 });
1763+
} finally {
1764+
vi.useRealTimers();
1765+
}
1766+
}),
1767+
).pipe(Effect.provide(layer)),
1768+
);

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,11 @@ const make = Effect.gen(function* () {
19091909
}
19101910
case "snapshot": {
19111911
return withScreencastsPaused(tab, async () =>
1912-
ServerBrowserPage.snapshot({ ...tab, ...(await snapshotRendering(tab)) }),
1912+
ServerBrowserPage.snapshot({
1913+
...tab,
1914+
renderScale: RENDER_SCALE,
1915+
...(tab.desktop ? { rendering: () => snapshotRendering(tab) } : {}),
1916+
}),
19131917
);
19141918
}
19151919
case "click": {

‎apps/server/src/preview/ServerBrowserPage.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ type SnapshotInput = {
468468
readonly page: Page;
469469
readonly cdp: CDPSession;
470470
readonly renderScale: number;
471+
readonly rendering?: () => Promise<Pick<SnapshotInput, "renderScale" | "viewport">>;
471472
/** For a page Playwright did not size, such as one the desktop draws. */
472473
readonly viewport?: Viewport;
473474
readonly consoleEntries: ReadonlyArray<PreviewAutomationConsoleEntry>;
@@ -564,13 +565,20 @@ export const snapshot = async (input: SnapshotInput): Promise<PreviewAutomationS
564565
// A page that changed mid-capture, like one rotating ads, has usually settled by a second
565566
// try. None starts late or after another snapshot began, since it would revoke the refs of
566567
// whichever snapshot came next.
567-
const captured = capture(input, deadline).catch((cause: unknown) =>
568-
Date.now() < deadline &&
569-
snapshotTurns.get(input.page) === turn &&
570-
!(cause instanceof Error && cause.message === VALUE_TOO_LONG)
571-
? capture(input, deadline)
572-
: Promise.reject(cause),
573-
);
568+
const captured = (async () => {
569+
if (input.rendering) {
570+
input = { ...input, ...(await input.rendering()) };
571+
// A late metrics reply must not begin a capture after viewers have resumed.
572+
if (Date.now() >= deadline) throw tooBusy();
573+
}
574+
return capture(input, deadline).catch((cause: unknown) =>
575+
Date.now() < deadline &&
576+
snapshotTurns.get(input.page) === turn &&
577+
!(cause instanceof Error && cause.message === VALUE_TOO_LONG)
578+
? capture(input, deadline)
579+
: Promise.reject(cause),
580+
);
581+
})();
574582
try {
575583
return await Promise.race([captured, expired]);
576584
} catch (cause) {

0 commit comments

Comments
 (0)