From 9d28e8ed138aff41e1fdacd37eef1f955fd3ebb0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 05:56:42 +0000 Subject: [PATCH 1/4] test(desktop): picture-in-picture after a preview webview is destroyed Reproduces #40146: the preview manager keeps a destroyed guest's webContentsId, so openPictureInPicture fails with PreviewWebContentsNotFoundError. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/preview/Manager.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 2a01977302594..16e31354ff824 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -1680,6 +1680,30 @@ describe("PreviewManager", () => { ), ); + effectIt.effect( + "does not open picture-in-picture against a destroyed webview's WebContents", + () => + withManager((manager) => + Effect.gen(function* () { + const guest = makeAttachingGuest(49); + fromId.mockImplementation((id) => + id === 49 && !guest.wc.isDestroyed() ? guest.wc : null, + ); + yield* manager.prepareWebview(guest.wc); + yield* manager.createTab("tab_destroyed_guest"); + yield* manager.registerWebview("tab_destroyed_guest", 49); + + guest.destroy(); + yield* Effect.yieldNow; + + const pictureInPicture = yield* Effect.flip( + manager.openPictureInPicture("tab_destroyed_guest"), + ); + expect(pictureInPicture._tag).not.toBe("PreviewWebContentsNotFoundError"); + }), + ), + ); + effectIt.effect("skips a guest destroyed while another guest's session opens", () => withManager((manager) => Effect.gen(function* () { From 56432b875020236b8f87bf205366ad871825fb44 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 06:18:09 +0000 Subject: [PATCH 2/4] fix(desktop): preview tabs drop a destroyed webview's WebContents When Electron destroyed a preview guest, the tab kept the dead webContentsId until a new registered, so picture-in-picture and other tab operations failed with PreviewWebContentsNotFoundError. The destroyed listener now detaches the guest from its tab, using the same cleanup navigate already ran for a dead guest, so the tab reports no webview and the renderer can register a new one. Fixes pingdotgg/t3code#40146 Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/preview/Manager.test.ts | 4 +- apps/desktop/src/preview/Manager.ts | 93 ++++++++++++++---------- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 16e31354ff824..9c15a0e0a4a95 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -1698,8 +1698,8 @@ describe("PreviewManager", () => { const pictureInPicture = yield* Effect.flip( manager.openPictureInPicture("tab_destroyed_guest"), - ); - expect(pictureInPicture._tag).not.toBe("PreviewWebContentsNotFoundError"); + ).pipe(Effect.orDie); + expect(pictureInPicture._tag).toBe("PreviewWebviewNotInitializedError"); }), ), ); diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index fbf70e12fd375..b106d793fffa0 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -2040,6 +2040,51 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function ); }); + // Clears a tab's dead guest so callers see "no webview" until the renderer registers a new one. + const detachDestroyedWebContents = Effect.fn("PreviewManager.detachDestroyedWebContents")( + function* (tabId: string, webContentsId: number) { + const expectedAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); + yield* withTabLifecycleLock( + tabId, + Effect.gen(function* () { + const currentTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); + const currentAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); + const currentWebContents = webContents.fromId(webContentsId); + if ( + currentTab?.webContentsId !== webContentsId || + currentAttachment !== expectedAttachment || + (currentWebContents && !currentWebContents.isDestroyed()) + ) { + return; + } + yield* Effect.all( + [ + detachControlSession(webContentsId), + detachListeners(webContentsId), + cancelPickElement(tabId), + ], + { concurrency: 3, discard: true }, + ); + const detached = yield* SynchronizedRef.modify(tabsRef, (tabs) => { + const current = tabs.get(tabId); + if (current?.webContentsId !== webContentsId) { + return [Option.none(), tabs] as const; + } + const { favicon: _favicon, ...currentWithoutFavicon } = current; + const next: PreviewTabState = { ...currentWithoutFavicon, webContentsId: null }; + return [ + Option.some(next), + replaceMap(tabs, (copy) => { + copy.set(tabId, next); + }), + ] as const; + }); + if (Option.isSome(detached)) yield* emitIfCurrent(tabId, detached.value); + }), + ); + }, + ); + // URL tabs can paint before registerWebview; set the background at attach time. const prepareWebview = Effect.fn("PreviewManager.prepareWebview")(function* ( wc: Electron.WebContents, @@ -2059,7 +2104,13 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function }); wc.once("destroyed", () => { guestLoadFailures.delete(webContentsId); - runFork(hideDestroyedGuest(webContentsId)); + runFork( + Effect.gen(function* () { + yield* hideDestroyedGuest(webContentsId); + const tabId = yield* tabIdForWebContents(webContentsId); + if (tabId) yield* detachDestroyedWebContents(tabId, webContentsId); + }), + ); runFork(detachControlSession(webContentsId)); }); yield* ensureControlSession(wc).pipe( @@ -2135,45 +2186,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function const webContentsId = pending.webContentsId; const wc = webContents.fromId(webContentsId); if (!wc || wc.isDestroyed()) { - const expectedAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); - yield* withTabLifecycleLock( - tabId, - Effect.gen(function* () { - const currentTab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); - const currentAttachment = (yield* Ref.get(attachedRef)).get(webContentsId); - const currentWebContents = webContents.fromId(webContentsId); - if ( - currentTab?.webContentsId !== webContentsId || - currentAttachment !== expectedAttachment || - (currentWebContents && !currentWebContents.isDestroyed()) - ) { - return; - } - yield* Effect.all( - [ - detachControlSession(webContentsId), - detachListeners(webContentsId), - cancelPickElement(tabId), - ], - { concurrency: 3, discard: true }, - ); - const detached = yield* SynchronizedRef.modify(tabsRef, (tabs) => { - const current = tabs.get(tabId); - if (current?.webContentsId !== webContentsId) { - return [Option.none(), tabs] as const; - } - const { favicon: _favicon, ...currentWithoutFavicon } = current; - const next: PreviewTabState = { ...currentWithoutFavicon, webContentsId: null }; - return [ - Option.some(next), - replaceMap(tabs, (copy) => { - copy.set(tabId, next); - }), - ] as const; - }); - if (Option.isSome(detached)) yield* emitIfCurrent(tabId, detached.value); - }), - ); + yield* detachDestroyedWebContents(tabId, webContentsId); return; } yield* leaveBusyPage(wc); From 8abcfca01b583bb45d50bebec39d329f15a28a26 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 08:06:06 +0000 Subject: [PATCH 3/4] fix(web): remount the browser preview after its webview is destroyed Clearing the destroyed guest's id stopped the PreviewWebContentsNotFoundError, but nothing reattached the , so the preview stayed blank with its float and pop-out controls disabled. HostedBrowserWebview now treats the desktop dropping its registered WebContents like a guest crash and remounts the webview through the existing crash-recovery backoff. The remount key moves to the webview's parent so React never removes a detached element. stopFindInPage also treats PreviewWebviewNotInitializedError as nothing left to stop, as it already did for a missing WebContents. Co-Authored-By: Claude Opus 5.5 --- apps/desktop/src/preview/Manager.ts | 1 + apps/web/src/browser/HostedBrowserWebview.tsx | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index b106d793fffa0..531e269a07f11 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -2261,6 +2261,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function Effect.catchTags({ PreviewTabNotFoundError: () => Effect.void, PreviewWebContentsNotFoundError: () => Effect.void, + PreviewWebviewNotInitializedError: () => Effect.void, }), ); diff --git a/apps/web/src/browser/HostedBrowserWebview.tsx b/apps/web/src/browser/HostedBrowserWebview.tsx index 023fb03c3aa17..b20d737095116 100644 --- a/apps/web/src/browser/HostedBrowserWebview.tsx +++ b/apps/web/src/browser/HostedBrowserWebview.tsx @@ -159,6 +159,7 @@ export function HostedBrowserWebview(props: { if (!clientSettingsHydrated || !webview || !config || !bridge) return; let disposed = false; let recoveryTimeout: ReturnType | null = null; + let registeredWebContentsId: number | null = null; const register = () => { const lease = tabLeaseRef.current; if (!lease) return; @@ -170,6 +171,7 @@ export function HostedBrowserWebview(props: { const webContentsId = webview.getWebContentsId(); if (Number.isInteger(webContentsId) && webContentsId > 0) { await bridge.registerWebview(runtimeTabId, webContentsId); + registeredWebContentsId = webContentsId; withDesktopTab(runtimeTabId, () => bridge.setShown(runtimeTabId, reportedShown())); } } catch {} @@ -198,6 +200,13 @@ export function HostedBrowserWebview(props: { dismissHostPopups(); } }; + // The desktop clears a destroyed guest's id; nothing reattaches this element, so remount it. + const unsubscribeState = bridge.onStateChange((changedTabId, state) => { + if (changedTabId !== runtimeTabId || registeredWebContentsId === null) return; + if (state.webContentsId !== null) return; + registeredWebContentsId = null; + recoverGuest(); + }); webview.addEventListener("did-attach", register); webview.addEventListener("dom-ready", register); webview.addEventListener("render-process-gone", recoverGuest); @@ -207,6 +216,7 @@ export function HostedBrowserWebview(props: { return () => { disposed = true; if (recoveryTimeout !== null) clearTimeout(recoveryTimeout); + unsubscribeState(); webview.removeEventListener("did-attach", register); webview.removeEventListener("dom-ready", register); webview.removeEventListener("render-process-gone", recoverGuest); @@ -374,6 +384,8 @@ export function HostedBrowserWebview(props: { /> ) : null}
Date: Sat, 21 Nov 2026 08:12:48 +0000 Subject: [PATCH 4/4] test(web): give the ElectronBrowserHost bridge mock onStateChange HostedBrowserWebview now subscribes to preview state to remount a destroyed webview; the real desktop bridge always provides it. Co-Authored-By: Claude Opus 5.5 --- apps/web/src/browser/ElectronBrowserHost.browser.test.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx b/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx index 29c217e064adc..d561254290427 100644 --- a/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx +++ b/apps/web/src/browser/ElectronBrowserHost.browser.test.tsx @@ -43,6 +43,7 @@ vi.mock(import("~/components/preview/previewBridge"), async (original) => ({ closeTab: mocks.closeTab, registerWebview: mocks.registerWebview, getPreviewConfig: mocks.getPreviewConfig, + onStateChange: () => () => {}, } as never, }));