Skip to content

Commit fec3b3a

Browse files
fix(preview): bound stalled screenshot captures (pingdotgg#25862)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 254e985 commit fec3b3a

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,54 @@ import {
1919
} from "vite-plus/test";
2020

2121
import * as ServerBrowserPage from "./ServerBrowserPage.ts";
22+
import { SessionControl } from "./SessionControl.ts";
2223

2324
const decodeEvaluateInput = Schema.decodeUnknownSync(PreviewAutomationEvaluateInput);
2425

26+
describe("server browser capture deadlines", () => {
27+
it("releases control after an unanswered capture and ignores its late reply", async () => {
28+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
29+
try {
30+
const captured = Promise.withResolvers<{ data: string }>();
31+
const send = vi.fn(async (method: string) =>
32+
method === "Page.captureScreenshot"
33+
? captured.promise
34+
: { result: { value: "still responsive" } },
35+
);
36+
const cdp = { send } as unknown as CDPSession;
37+
const control = new SessionControl("agent");
38+
let outcome: unknown;
39+
const capture = control
40+
.agent("agent", () => ServerBrowserPage.captureViewport(cdp, { format: "png" }))
41+
.catch((cause: unknown) => {
42+
outcome = cause;
43+
});
44+
const evaluation = control.agent("agent", () =>
45+
ServerBrowserPage.evaluate(cdp, { expression: "document.title" }, 1_000),
46+
);
47+
48+
await vi.advanceTimersByTimeAsync(3_000);
49+
expect(outcome).toMatchObject({
50+
tag: "PreviewAutomationExecutionError",
51+
message: expect.stringContaining("not producing screenshot frames"),
52+
});
53+
await capture;
54+
expect(await evaluation).toBe("still responsive");
55+
expect(vi.getTimerCount()).toBe(0);
56+
57+
captured.resolve({ data: "late-frame" });
58+
await captured.promise;
59+
expect(outcome).toBeInstanceOf(ServerBrowserPage.ServerBrowserOperationError);
60+
expect(send.mock.calls.map(([method]) => method)).toEqual([
61+
"Page.captureScreenshot",
62+
"Runtime.evaluate",
63+
]);
64+
} finally {
65+
vi.useRealTimers();
66+
}
67+
});
68+
});
69+
2570
describe("server browser element refs", () => {
2671
let browser: Browser;
2772
let context: BrowserContext;

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ const isBrowserControlInterrupted = Schema.is(BrowserControlInterrupted);
2929
const MAX_EVALUATION_BYTES = 64_000;
3030
const MAX_VISIBLE_TEXT_LENGTH = 20_000;
3131
const MAX_SCREENSHOT_WIDTH = 1280;
32+
// Two attempts must fit in a snapshot's 10-second deadline.
33+
const SCREENSHOT_TIMEOUT_MS = 3_000;
3234
const WAIT_POLL_MS = 100;
3335
export const DIAGNOSTIC_BUFFER_LIMIT = 200;
3436

@@ -468,10 +470,27 @@ export const captureViewport = async (
468470
cdp: CDPSession,
469471
options: { readonly format: "png" | "jpeg"; readonly quality?: number },
470472
) => {
471-
const { data } = await cdp.send("Page.captureScreenshot", {
473+
const capture = cdp.send("Page.captureScreenshot", {
472474
format: options.format,
473475
...(options.quality === undefined ? {} : { quality: options.quality }),
474476
});
477+
let timer: ReturnType<typeof setTimeout> | undefined;
478+
const expired = new Promise<never>((_, reject) => {
479+
timer = setTimeout(
480+
() =>
481+
reject(
482+
new ServerBrowserOperationError({
483+
tag: "PreviewAutomationExecutionError",
484+
message:
485+
"The browser is not producing screenshot frames. For a desktop tab, show or restore the T3 Code window and try again.",
486+
}),
487+
),
488+
SCREENSHOT_TIMEOUT_MS,
489+
);
490+
});
491+
// Hidden native guests can leave CDP captures pending forever. This deadline also
492+
// releases a viewer's still-capture lock, which an agent snapshot may be waiting for.
493+
const { data } = await Promise.race([capture, expired]).finally(() => clearTimeout(timer));
475494
return data;
476495
};
477496

0 commit comments

Comments
 (0)