@@ -1023,13 +1023,22 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
10231023 copy.delete(webContentsId);
10241024 }),
10251025 ]);
1026- if (control) {
1027- if (closedServerTab) browserHost.detach(closedServerTab);
1028- for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
1029- if (tab.webContentsId === webContentsId && tab.serverTab) browserHost.detach(tab.serverTab);
1026+ if (control) yield* releaseControlSession(control, closedServerTab);
1027+ });
1028+
1029+ // Closes a session already removed from `controlSessionsRef`. The server can
1030+ // only drive a tab while the desktop holds its debugger.
1031+ const releaseControlSession = Effect.fnUntraced(function* (
1032+ control: BrowserControlSession,
1033+ closedServerTab?: PreviewTabState["serverTab"],
1034+ ) {
1035+ if (closedServerTab) browserHost.detach(closedServerTab);
1036+ for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
1037+ if (tab.webContentsId === control.webContentsId && tab.serverTab) {
1038+ browserHost.detach(tab.serverTab);
10301039 }
1031- yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
10321040 }
1041+ yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
10331042 });
10341043
10351044 const ensureControlSession = Effect.fn("PreviewManager.ensureControlSession")(function* (
@@ -1141,23 +1150,36 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
11411150 const onMessage: BrowserControlSession["onMessage"] = (_event, method, params) => {
11421151 runFork(handleDebuggerMessage(method, params));
11431152 };
1144- yield* Scope.addFinalizer(
1145- scope,
1146- attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => {
1147- wcDebugger.off("message", onMessage);
1148- if (wcDebugger.isAttached()) wcDebugger.detach();
1149- }).pipe(Effect.ignore),
1150- );
11511153 const control: BrowserControlSession = {
11521154 webContentsId: wc.id,
11531155 debugger: wcDebugger,
11541156 semaphore,
11551157 scope,
11561158 onMessage,
11571159 };
1160+ // Electron emits `detach` when Chromium closes the debugger session on its
1161+ // own. The cached session would then fail every command until the webview
1162+ // is replaced.
1163+ let dropped = false;
1164+ const onDetach = () => {
1165+ dropped = true;
1166+ runFork(recoverDetachedControlSession(wc, control));
1167+ };
1168+ // The listener comes off before detach() so a detach this manager requested
1169+ // never looks like a dropped one. A dropped session has nothing left to
1170+ // detach: `wc.debugger` is shared, so detaching here would hit a successor.
1171+ yield* Scope.addFinalizer(
1172+ scope,
1173+ attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => {
1174+ wcDebugger.off("message", onMessage);
1175+ wcDebugger.off("detach", onDetach);
1176+ if (!dropped && wcDebugger.isAttached()) wcDebugger.detach();
1177+ }).pipe(Effect.ignore),
1178+ );
11581179 const initialize = Effect.fn("PreviewManager.initializeControlSession")(function* () {
11591180 yield* attempt({ operation: "attachDebuggerListeners", webContentsId: wc.id }, () => {
11601181 wcDebugger.on("message", onMessage);
1182+ wcDebugger.on("detach", onDetach);
11611183 wcDebugger.attach("1.3");
11621184 });
11631185 // Without this, dark-scheme pages with no background of their own (text/plain, e.g.
@@ -2356,6 +2378,30 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
23562378 }
23572379 }).pipe(Effect.ignore);
23582380
2381+ // Only the session that saw the detach is replaced: by the time the event is
2382+ // handled the guest may already hold a newer one, which must stay.
2383+ const recoverDetachedControlSession = Effect.fn("PreviewManager.recoverDetachedControlSession")(
2384+ function* (wc: Electron.WebContents, detached: BrowserControlSession) {
2385+ const removed = yield* SynchronizedRef.modify(controlSessionsRef, (sessions) =>
2386+ sessions.get(wc.id) === detached
2387+ ? ([
2388+ true,
2389+ replaceMap(sessions, (copy) => {
2390+ copy.delete(wc.id);
2391+ }),
2392+ ] as const)
2393+ : ([false, sessions] as const),
2394+ );
2395+ if (!removed) return;
2396+ yield* releaseControlSession(detached);
2397+ if (wc.isDestroyed()) return;
2398+ const tabId = yield* tabIdForWebContents(wc.id);
2399+ if (tabId === null) return;
2400+ yield* restoreControlSession(tabId, wc);
2401+ },
2402+ (recover) => Effect.ignore(recover),
2403+ );
2404+
23592405 const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* (
23602406 tabId: string,
23612407 colorScheme: DesktopPreviewColorScheme,
0 commit comments