Skip to content
TypeScript

fix(desktop): a stalled capture no longer freezes picture-in-picture (#24057)

Batuhan1999ae45ccd2ccab
diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts
index f19079e46bc..2998c95d818 100644
--- a/apps/desktop/src/preview/Manager.test.ts
+++ b/apps/desktop/src/preview/Manager.test.ts
@@ -2201,6 +2201,48 @@ describe("PreviewManager", () => {
),
);
+ effectIt.effect("shows picture-in-picture and keeps capturing after stalled captures", () =>
+ withManager((manager) =>
+ Effect.gen(function* () {
+ const image = {
+ toJPEG: () => Buffer.from("preview-frame"),
+ getSize: () => ({ width: 1280, height: 720 }),
+ };
+ const stalledInitial = Promise.withResolvers<typeof image>();
+ const stalledLoop = Promise.withResolvers<typeof image>();
+ const capturePage = vi
+ .fn<() => Promise<typeof image>>()
+ .mockImplementationOnce(() => stalledInitial.promise)
+ .mockImplementationOnce(() => stalledLoop.promise)
+ .mockResolvedValue(image);
+ fromId.mockReturnValue(makeTestPreviewWebContents(capturePage));
+ const { pictureInPictureWindow, send } = makeTestPictureInPictureWindow();
+ browserWindowConstructor.mockImplementation(function () {
+ return pictureInPictureWindow;
+ });
+ yield* manager.createTab("tab_pip_stalled_capture");
+ yield* manager.registerWebview("tab_pip_stalled_capture", 42);
+
+ try {
+ const open = yield* manager
+ .openPictureInPicture("tab_pip_stalled_capture")
+ .pipe(Effect.forkChild({ startImmediately: true }));
+ yield* TestClock.adjust(1_000);
+ expect(pictureInPictureWindow.showInactive).toHaveBeenCalledOnce();
+ yield* Fiber.join(open);
+
+ expect(send).not.toHaveBeenCalled();
+ yield* TestClock.adjust(500);
+ expect(send).toHaveBeenCalled();
+ } finally {
+ // Release stalled native calls after a failing assertion against unchanged base.
+ stalledInitial.resolve(image);
+ stalledLoop.resolve(image);
+ }
+ }),
+ ),
+ );
+
effectIt.effect("rejects picture-in-picture when its webview changes during initialization", () =>
withManager((manager) =>
Effect.gen(function* () {
diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts
index 036c0d4afd9..b1f7a6d9391 100644
--- a/apps/desktop/src/preview/Manager.ts
+++ b/apps/desktop/src/preview/Manager.ts
@@ -2426,13 +2426,13 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
if (!captureSession?.consumers.has("picture-in-picture") || captureSession.scope === null)
return;
const wc = yield* requireWebContents(tabId);
- const image = yield* attemptPromise(
- {
- operation: "frameCapture.capturePage",
- tabId,
- webContentsId: wc.id,
- },
- () => wc.capturePage(),
+ const captureContext = { operation: "frameCapture.capturePage", tabId, webContentsId: wc.id };
+ const image = yield* attemptPromise(captureContext, () => wc.capturePage()).pipe(
+ Effect.timeout(CAPTURE_PAGE_ATTEMPT_TIMEOUT_MS),
+ Effect.catchTags({
+ TimeoutError: (cause) =>
+ Effect.fail(new PreviewOperationError({ ...captureContext, cause })),
+ }),
);
const currentCaptureSession = yield* Effect.all(
[SynchronizedRef.get(frameCaptureSessionsRef), SynchronizedRef.get(tabsRef)],