Skip to content

Commit 35054dc

Browse files
committed
fix(server): a stalled viewer screencast no longer locks its preview tab
Viewer screencast commands and the viewer's still capture waited on Chromium with no deadline. One unanswered Page.startScreencast kept attachViewer from returning, so the panel never connected, and every later snapshot and viewer on the tab waited behind it. Each viewer command now gives up after five seconds.
1 parent 2cb5cdf commit 35054dc

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const AGENT_CURSOR_MOVE_MS = 160;
105105
const AGENT_CURSOR_CLICK_LEAD_MS = 40;
106106
/** A dialog blocks page script, so a desktop size read gives up after this. */
107107
const DESKTOP_VIEWPORT_READ_TIMEOUT = "1 second";
108+
const VIEWER_COMMAND_TIMEOUT = "5 seconds";
108109

109110
const sleepUntil = (deadline: number) =>
110111
new Promise<void>((resolve) => setTimeout(resolve, Math.max(0, deadline - Date.now())));
@@ -1787,6 +1788,14 @@ const make = Effect.gen(function* () {
17871788
return started;
17881789
};
17891790

1791+
const withinViewerDeadline = <A>(command: Promise<A>): Promise<A | null> =>
1792+
runPromise(
1793+
Effect.promise(() => command.catch(() => null)).pipe(
1794+
Effect.timeoutOption(VIEWER_COMMAND_TIMEOUT),
1795+
Effect.map(Option.getOrNull),
1796+
),
1797+
);
1798+
17901799
const withScreencastsPaused = <A>(tab: ServerTab, capture: () => Promise<A>): Promise<A> =>
17911800
withCaptureLock(tab, async () => {
17921801
tab.capturing += 1;
@@ -2560,15 +2569,15 @@ const make = Effect.gen(function* () {
25602569
screencastScale = scale;
25612570
screencastParams = screencastParams.then(async () => {
25622571
if (tab.capturing > 0) return;
2563-
await session.send("Page.stopScreencast").catch(constVoid);
2564-
await session
2565-
.send("Page.startScreencast", {
2572+
await withinViewerDeadline(session.send("Page.stopScreencast"));
2573+
await withinViewerDeadline(
2574+
session.send("Page.startScreencast", {
25662575
format: "jpeg",
25672576
quality: screencastScale < 1 ? Math.min(quality, SCREENCAST_MOTION_QUALITY) : quality,
25682577
maxWidth: Math.max(1, Math.round(input.maxWidth * screencastScale)),
25692578
maxHeight: Math.max(1, Math.round(input.maxHeight * screencastScale)),
2570-
})
2571-
.catch(constVoid);
2579+
}),
2580+
);
25722581
});
25732582
return screencastParams;
25742583
};
@@ -2601,9 +2610,9 @@ const make = Effect.gen(function* () {
26012610
}
26022611
},
26032612
pause: () => {
2604-
screencastParams = screencastParams.then(() =>
2605-
session.send("Page.stopScreencast").then(constVoid, constVoid),
2606-
);
2613+
screencastParams = screencastParams
2614+
.then(() => withinViewerDeadline(session.send("Page.stopScreencast")))
2615+
.then(constVoid);
26072616
return screencastParams;
26082617
},
26092618
resume: () => startScreencast(screencastScale),
@@ -2630,7 +2639,9 @@ const make = Effect.gen(function* () {
26302639
pushFileChooser(tab);
26312640
const pushStill = async () => {
26322641
const data = await withCaptureLock(tab, () =>
2633-
ServerBrowserPage.captureViewport(session, { format: "jpeg", quality }),
2642+
withinViewerDeadline(
2643+
ServerBrowserPage.captureViewport(session, { format: "jpeg", quality }),
2644+
),
26342645
).catch(() => null);
26352646
if (data) viewer.push({ _tag: "frame", data: Buffer.from(data, "base64"), ack: Effect.void });
26362647
};

0 commit comments

Comments
 (0)