The best way to control Claude Code, Codex and more from any computer or phone
fix(preview): remote viewers recover from stalled first captures (#25381)
Julius Marminge committed
Nov 12, 2026 at 20:11 UTC
97cfc07759fed2a67edef22de4370ed74ff40030
3 files changed
+56
-6
apps/mobile/src/features/browser/PreviewStreamWebView.tsx
+1
-1
@@ -405,7 +405,7 @@ function PreviewStreamDocumentView({
405
onPictureInPicture?.(message, message.detail);
406
return;
407
case "status":
408
- clearBootstrapTimer();
408
+ if (message.status !== "connecting") clearBootstrapTimer();
409
if (message.status === "error") {
410
fail(message.detail ?? "Browser stream failed.");
411
return;
apps/server/src/preview/ServerBrowser.test.ts
+39
-1
@@ -114,6 +114,7 @@ function makeSession(
114
if (method === "Page.getNavigationHistory") return { currentIndex: 0, entries: [{}] };
115
if (method === "Page.getLayoutMetrics") return { cssVisualViewport: { pageX: 0, pageY: 0 } };
116
if (method === "Page.captureScreenshot") {
117
+ if ((input as { format: string }).format === "jpeg" && heldStill) await heldStill();
118
// A snapshot sizes its screenshot from the PNG header; viewers get JPEG frames as-is.
119
return { data: (input as { format: string }).format === "png" ? PNG_HEADER : "ZnJhbWU=" };
120
}
@@ -384,6 +385,7 @@ let encoderPageOverride: Page | undefined;
385
const encoderStreams: Array<{ frameRate: number | undefined; bitsPerSecond: number }> = [];
386
const contextRequests: Array<{ profileId: string; isolated: boolean }> = [];
387
let contextGate: PromiseWithResolvers<void> | null = null;
388
+let heldStill: (() => Promise<void>) | null = null;
389
let pageCopy = "";
390
let keyCopy = "";
391
let focusedFrame: ReturnType<typeof makeFrame> | null = null;
@@ -568,6 +570,7 @@ beforeEach(() => {
570
encoderStreams.length = 0;
571
contextRequests.length = 0;
572
contextGate = null;
573
+ heldStill = null;
574
contextFailure = null;
575
pageSetupFailure = null;
576
initialNavigation = null;
@@ -1130,6 +1133,38 @@ it.live("enforces provider ownership and explicit targets when a session has mul
1133
).pipe(Effect.provide(layer)),
1134
);
1135
1136
+it.effect("a first still that never returns does not stall the tab's later captures", () =>
1137
+ Effect.scoped(
1138
+ Effect.gen(function* () {
1139
+ const { browser, broker, tabId } = yield* ready;
1140
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
1141
+ yield* Effect.addFinalizer(() => Effect.sync(() => vi.useRealTimers()));
1142
+ const capturing = Promise.withResolvers<void>();
1143
+ const stuck = Promise.withResolvers<void>();
1144
+ heldStill = () => {
1145
+ heldStill = null;
1146
+ capturing.resolve();
1147
+ return stuck.promise;
1148
+ };
1149
+ yield* Effect.addFinalizer(() => Effect.sync(() => stuck.resolve()));
1150
+ yield* browser.attachViewer(viewerInput(tabId, true));
1151
+ yield* Effect.promise(() => capturing.promise);
1152
+ const snapshotting = yield* broker
1153
+ .invoke<PreviewAutomationSnapshot>({ scope, tabId, operation: "snapshot", input: {} })
1154
+ .pipe(Effect.forkScoped);
1155
+ yield* Effect.promise(() => vi.advanceTimersByTimeAsync(2_000));
1156
+ const snapshot = yield* Fiber.join(snapshotting);
1157
+ expect(snapshot.accessibilityTree).toContain("delete");
1158
+ const second = yield* browser.attachViewer(viewerInput(tabId, false));
1159
+ const frame = yield* Stream.fromQueue(second.output).pipe(
1160
+ Stream.filter((output) => output._tag === "frame"),
1161
+ Stream.runHead,
1162
+ );
1163
+ expect(Option.isSome(frame)).toBe(true);
1164
+ }),
1165
+ ).pipe(Effect.provide(layer)),
1166
+);
1167
+
1168
it.live("streams to a read-only viewer without allowing takeover, input, or viewport changes", () =>
1169
Effect.scoped(
1170
Effect.gen(function* () {
@@ -1148,7 +1183,10 @@ it.live("streams to a read-only viewer without allowing takeover, input, or view
1183
yield* viewer.input(message);
1184
expect(page.setViewportSize).not.toHaveBeenCalled();
1185
expect(session.send.mock.calls.some(([method]) => method.startsWith("Input."))).toBe(false);
1151
- const outputs = yield* Queue.takeAll(viewer.output);
1186
+ const outputs = yield* Stream.fromQueue(viewer.output).pipe(
1187
+ Stream.takeUntil((output) => output._tag === "frame"),
1188
+ Stream.runCollect,
1189
+ );
1190
expect(outputs).toContainEqual(
1191
expect.objectContaining({ _tag: "frame", data: Buffer.from("frame") }),
1192
);
apps/server/src/preview/ServerBrowser.ts
+16
-4
@@ -111,6 +111,8 @@ const SCREENCAST_MOTION_WINDOW_MS = 300;
111
const SCREENCAST_MOTION_QUALITY = 50;
112
const HOST_RECONNECT_DELAY = "1 second";
113
const DESKTOP_ATTACH_TIMEOUT = "10 seconds";
114
+// A viewer still is best-effort; a capture Chromium never answers must not hold the capture lock.
115
+const VIEWER_STILL_TIMEOUT_MS = 2_000;
116
const VIEWER_OUTPUT_LIMIT = 64;
117
const RECORDING_SCREENCAST = { format: "jpeg", quality: 90, everyNthFrame: 1 } as const;
118
const RECORDING_BINARY_EXPORT_MIN_BYTES = 8 * 1024 * 1024;
@@ -2945,9 +2947,19 @@ const make = Effect.gen(function* () {
2947
broadcastControl(tab);
2948
pushFileChooser(tab);
2949
const pushStill = async () => {
2948
- const data = await withCaptureLock(tab, () =>
2949
- ServerBrowserPage.captureViewport(session, { format: "jpeg", quality }),
2950
- ).catch(() => null);
2950
+ const data = await withCaptureLock(tab, async () => {
2951
+ let timer: ReturnType<typeof setTimeout> | undefined;
2952
+ try {
2953
+ return await Promise.race([
2954
+ ServerBrowserPage.captureViewport(session, { format: "jpeg", quality }),
2955
+ new Promise<null>((resolve) => {
2956
+ timer = setTimeout(() => resolve(null), VIEWER_STILL_TIMEOUT_MS);
2957
+ }),
2958
+ ]);
2959
+ } finally {
2960
+ clearTimeout(timer);
2961
+ }
2962
+ }).catch(() => null);
2963
if (data) viewer.push({ _tag: "frame", data: Buffer.from(data, "base64"), ack: Effect.void });
2964
};
2965
let settleTimer: ReturnType<typeof setTimeout> | null = null;
@@ -3005,7 +3017,7 @@ const make = Effect.gen(function* () {
3017
yield* Effect.promise(() => startScreencast(1));
3018
// An idle page does not repaint for a new screencast, so the viewer
3019
// starts from a still unless a live frame beat it.
3008
- if (!screencastStarted) yield* Effect.promise(pushStill);
3020
+ if (!screencastStarted) yield* Effect.forkScoped(Effect.promise(pushStill));
3021
return {
3022
output,
3023
input: (raw: unknown) =>