Skip to content

Commit cd4c587

Browse files
tobymarksclaude
andcommitted
fix(desktop): reattach only the dropped session, and after app-menu DevTools close
Addresses review: the session swap is now one atomic step, so a newer session for the guest is never released by mistake. DevTools opened outside the manager hold the debugger, so the tab reattaches once they close. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c7e5f12 commit cd4c587

2 files changed

Lines changed: 63 additions & 11 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,14 +1780,15 @@ describe("PreviewManager", () => {
17801780
);
17811781
const debuggerListeners = new Map<string, Set<() => void>>();
17821782
let chromiumDetaches = 0;
1783+
let devToolsOpen = false;
17831784
let destroyed = false;
17841785
const wc = {
17851786
id,
17861787
isDestroyed: () => destroyed,
17871788
isDevToolsOpened: () => {
17881789
// Electron throws from native methods once a WebContents is destroyed.
17891790
if (destroyed) throw new TypeError("Object has been destroyed");
1790-
return false;
1791+
return devToolsOpen;
17911792
},
17921793
getType: () => "webview",
17931794
getURL: () => "http://localhost:5173/README.md",
@@ -1822,6 +1823,10 @@ describe("PreviewManager", () => {
18221823
},
18231824
},
18241825
};
1826+
// Snapshot like an EventEmitter, so a listener added during emit waits for the next one.
1827+
const emitDebuggerDetach = () => {
1828+
for (const listener of Array.from(debuggerListeners.get("detach") ?? [])) listener();
1829+
};
18251830
return {
18261831
wc: wc as unknown as Electron.WebContents,
18271832
attach,
@@ -1830,7 +1835,17 @@ describe("PreviewManager", () => {
18301835
/** Chromium dropping the debugger without a detach() call. */
18311836
chromiumDetach: () => {
18321837
chromiumDetaches += 1;
1833-
for (const listener of debuggerListeners.get("detach") ?? []) listener();
1838+
emitDebuggerDetach();
1839+
},
1840+
/** DevTools opened outside the manager, e.g. from the app menu. */
1841+
openDevTools: () => {
1842+
devToolsOpen = true;
1843+
chromiumDetaches += 1;
1844+
emitDebuggerDetach();
1845+
},
1846+
closeDevTools: () => {
1847+
devToolsOpen = false;
1848+
listeners.get("devtools-closed")?.();
18341849
},
18351850
destroy: () => {
18361851
destroyed = true;
@@ -1887,6 +1902,16 @@ describe("PreviewManager", () => {
18871902
yield* Effect.yieldNow;
18881903
yield* Effect.yieldNow;
18891904
expect(guest.attach).toHaveBeenCalledTimes(2);
1905+
1906+
// DevTools hold the debugger until they close.
1907+
guest.openDevTools();
1908+
yield* Effect.yieldNow;
1909+
yield* Effect.yieldNow;
1910+
expect(guest.attach).toHaveBeenCalledTimes(2);
1911+
guest.closeDevTools();
1912+
yield* Effect.yieldNow;
1913+
yield* Effect.yieldNow;
1914+
expect(guest.attach).toHaveBeenCalledTimes(3);
18901915
}),
18911916
),
18921917
);

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,14 +1042,22 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
10421042
copy.delete(webContentsId);
10431043
}),
10441044
]);
1045-
if (control) {
1046-
// The server can only drive a tab while the desktop holds its debugger.
1047-
if (closedServerTab) browserHost.detach(closedServerTab);
1048-
for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
1049-
if (tab.webContentsId === webContentsId && tab.serverTab) browserHost.detach(tab.serverTab);
1045+
if (control) yield* releaseControlSession(control, closedServerTab);
1046+
});
1047+
1048+
/** Closes a session already taken out of `controlSessionsRef`. */
1049+
const releaseControlSession = Effect.fnUntraced(function* (
1050+
control: BrowserControlSession,
1051+
closedServerTab?: PreviewTabState["serverTab"],
1052+
) {
1053+
// The server can only drive a tab while the desktop holds its debugger.
1054+
if (closedServerTab) browserHost.detach(closedServerTab);
1055+
for (const tab of (yield* SynchronizedRef.get(tabsRef)).values()) {
1056+
if (tab.webContentsId === control.webContentsId && tab.serverTab) {
1057+
browserHost.detach(tab.serverTab);
10501058
}
1051-
yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
10521059
}
1060+
yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
10531061
});
10541062

10551063
const ensureControlSession = Effect.fn("PreviewManager.ensureControlSession")(function* (
@@ -2408,11 +2416,30 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
24082416
detached: BrowserControlSession,
24092417
) =>
24102418
Effect.gen(function* () {
2411-
if ((yield* SynchronizedRef.get(controlSessionsRef)).get(wc.id) !== detached) return;
2412-
yield* detachControlSession(wc.id);
2419+
const removed = yield* SynchronizedRef.modify(controlSessionsRef, (sessions) =>
2420+
sessions.get(wc.id) === detached
2421+
? ([
2422+
true,
2423+
replaceMap(sessions, (copy) => {
2424+
copy.delete(wc.id);
2425+
}),
2426+
] as const)
2427+
: ([false, sessions] as const),
2428+
);
2429+
if (!removed) return;
2430+
yield* releaseControlSession(detached);
24132431
if (wc.isDestroyed()) return;
24142432
const tabId = yield* tabIdForWebContents(wc.id);
2415-
if (tabId !== null) yield* restoreControlSession(tabId, wc);
2433+
if (tabId === null) return;
2434+
// DevTools opened from the app menu take the debugger; attach again once
2435+
// they close, as openDevTools does for the ones it opens.
2436+
if (wc.isDevToolsOpened()) {
2437+
wc.once("devtools-closed", () => {
2438+
if (!wc.isDestroyed()) runFork(restoreControlSession(tabId, wc));
2439+
});
2440+
return;
2441+
}
2442+
yield* restoreControlSession(tabId, wc);
24162443
}).pipe(Effect.ignore);
24172444

24182445
const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* (

0 commit comments

Comments
 (0)