Skip to content

Commit f5d52e7

Browse files
committed
fix(desktop): don't let an unresponsive renderer hold preview screenshots
1 parent 4cc0368 commit f5d52e7

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Fiber from "effect/Fiber";
66
import * as Queue from "effect/Queue";
77
import * as Schema from "effect/Schema";
88
import * as Stream from "effect/Stream";
9+
import * as TestClock from "effect/testing/TestClock";
910
import * as NodeEvents from "node:events";
1011

1112
import * as DesktopBrowserHost from "./DesktopBrowserHost.ts";
@@ -30,7 +31,10 @@ const command = (
3031
}),
3132
);
3233

33-
const makeDebuggee = (prepareSurface: () => Promise<unknown> = async () => true) => {
34+
const makeDebuggee = (
35+
prepareSurface: () => Promise<unknown> = async () => true,
36+
restoreSurface: () => Promise<unknown> = async () => true,
37+
) => {
3438
const emitter = new NodeEvents.EventEmitter();
3539
const pending: Array<(cause?: Error) => void> = [];
3640
const sent: Array<unknown> = [];
@@ -55,7 +59,7 @@ const makeDebuggee = (prepareSurface: () => Promise<unknown> = async () => true)
5559
isDestroyed: () => false,
5660
executeJavaScript: (expression: string) => {
5761
surfaceRequests.push(expression);
58-
return surfaceRequests.length === 1 ? prepareSurface() : Promise.resolve(true);
62+
return expression.endsWith(", true)") ? prepareSurface() : restoreSurface();
5963
},
6064
},
6165
getURL: () => "http://localhost/",
@@ -172,6 +176,39 @@ describe("DesktopBrowserHost", () => {
172176
}).pipe(Effect.scoped),
173177
);
174178

179+
it.effect("sends the screenshot when the renderer never answers readiness", () =>
180+
Effect.gen(function* () {
181+
const host = yield* DesktopBrowserHost.make;
182+
const debuggee = makeDebuggee(() => new Promise(() => {}));
183+
host.attach(key, debuggee.tab);
184+
const replies = yield* watchReplies(host);
185+
186+
const sent = debuggee.nextSend();
187+
yield* command(host, 1, "Page.captureScreenshot");
188+
yield* TestClock.adjust("2 seconds");
189+
yield* Effect.promise(() => sent);
190+
debuggee.releaseFirst();
191+
expect(yield* Queue.take(replies)).toBe(1);
192+
}).pipe(Effect.scoped),
193+
);
194+
195+
it.effect("answers and moves on when the renderer never answers restoring the surface", () =>
196+
Effect.gen(function* () {
197+
const host = yield* DesktopBrowserHost.make;
198+
const debuggee = makeDebuggee(undefined, () => new Promise(() => {}));
199+
host.attach(key, debuggee.tab);
200+
const replies = yield* watchReplies(host);
201+
202+
yield* sendScreenshot(host, debuggee, 1);
203+
yield* command(host, 2, "Page.captureScreenshot");
204+
const secondSent = debuggee.nextSend();
205+
debuggee.releaseFirst();
206+
expect(yield* Queue.take(replies)).toBe(1);
207+
yield* Effect.promise(() => secondSent);
208+
expect(debuggee.sent).toEqual([1, 2]);
209+
}).pipe(Effect.scoped),
210+
);
211+
175212
it.effect("announces tabs already attached to a backend that starts later", () =>
176213
Effect.gen(function* () {
177214
const host = yield* DesktopBrowserHost.make;

‎apps/desktop/src/preview/DesktopBrowserHost.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const lineEncoder = new TextEncoder();
2828
const AGENT_INPUT_COMMAND =
2929
/"method":"(?:Input\.|Page\.navigate"|Runtime\.(?:evaluate|callFunctionOn)")/;
3030
const AGENT_DOWNLOAD_WINDOW_MS = 5_000;
31+
// The renderer gives up on readiness after 1s; this only catches a renderer that never answers.
32+
const CAPTURE_SURFACE_READY_TIMEOUT = "2 seconds";
3133

3234
export interface DesktopBrowserTabKey {
3335
readonly threadId: string;
@@ -86,6 +88,7 @@ export const make = Effect.gen(function* () {
8688
readonly y: number;
8789
}>(16);
8890
const runFork = Effect.runForkWith(yield* Effect.context<never>());
91+
const runPromise = Effect.runPromiseWith(yield* Effect.context<never>());
8992
const tabs = new Map<string, AttachedTab>();
9093
const emit = (event: DesktopBrowserEventType) => runFork(PubSub.publish(outbox, event));
9194

@@ -143,13 +146,20 @@ export const make = Effect.gen(function* () {
143146
// receives its new bounds. CDP can otherwise wait forever for a composited frame.
144147
// A window that isn't painting, like a minimized one, never reports ready, yet its
145148
// on-screen tab can still be captured, so the screenshot is sent either way.
146-
await setSurface(true).catch(() => false);
149+
// A renderer that never answers would hold this screenshot and every one queued behind it.
150+
await runPromise(
151+
Effect.tryPromise(() => setSurface(true)).pipe(
152+
Effect.timeoutOption(CAPTURE_SURFACE_READY_TIMEOUT),
153+
Effect.ignore,
154+
),
155+
);
147156
if (tab.relay !== relay || tab.screenshotTurn !== turn || wc.isDestroyed()) {
148157
throw new Error("The preview connection that asked for this screenshot closed.");
149158
}
150159
return await capture();
151160
} finally {
152-
if (!host.isDestroyed()) await setSurface(false).catch(() => undefined);
161+
// The renderer bounds its own lease, so the result needn't wait for its answer.
162+
if (!host.isDestroyed()) void setSurface(false).catch(() => undefined);
153163
}
154164
};
155165

0 commit comments

Comments
 (0)