@devries-cameron20 / t3code-1 / commits / 0b37829da9
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(desktop): reattach a browser tab whose debugger Chromium dropped (#17525)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

tobymarks committed Oct 19, 2026 at 07:03 UTC 0b37829da959ddde47abdcec691ac8909d73744c
2 files changed +92 -15
apps/desktop/src/preview/Manager.test.ts
+34 -3
@@ -1886,6 +1886,8 @@ describe("PreviewManager", () => {
1886 const sendCommand = vi.fn<(method: string, params?: unknown) => Promise<unknown>>(
1887 async () => undefined,
1888 );
1889 + const detachListeners = new Set<() => void>();
1890 + let chromiumDetaches = 0;
1891 let destroyed = false;
1892 const wc = {
1893 id,
@@ -1914,19 +1916,30 @@ describe("PreviewManager", () => {
1916 setIgnoreMenuShortcuts: vi.fn(),
1917 setWindowOpenHandler: vi.fn(),
1918 debugger: {
1917 - isAttached: () => attach.mock.calls.length > detach.mock.calls.length,
1919 + isAttached: () => attach.mock.calls.length > detach.mock.calls.length + chromiumDetaches,
1920 attach,
1921 detach,
1922 sendCommand,
1921 - on: vi.fn(),
1922 - off: vi.fn(),
1923 + on: (event: string, listener: () => void) => {
1924 + if (event === "detach") detachListeners.add(listener);
1925 + },
1926 + off: (event: string, listener: () => void) => {
1927 + if (event === "detach") detachListeners.delete(listener);
1928 + },
1929 },
1930 };
1931 + /** Chromium dropping the debugger without a detach() call. */
1932 + const chromiumDetach = () => {
1933 + chromiumDetaches += 1;
1934 + // Snapshot like an EventEmitter, so a listener added during emit waits for the next one.
1935 + for (const listener of Array.from(detachListeners)) listener();
1936 + };
1937 return {
1938 wc: wc as unknown as Electron.WebContents,
1939 attach,
1940 detach,
1941 sendCommand,
1942 + chromiumDetach,
1943 destroy: () => {
1944 destroyed = true;
1945 listeners.get("destroyed")?.();
@@ -1965,6 +1978,24 @@ describe("PreviewManager", () => {
1978 ),
1979 );
1980
1981 + effectIt.effect("reattaches a debugger that Chromium dropped on its own", () =>
1982 + withManager((manager) =>
1983 + Effect.gen(function* () {
1984 + const guest = makeAttachingGuest(48);
1985 + fromId.mockReturnValue(guest.wc);
1986 + yield* manager.createTab("tab_detached");
1987 + yield* manager.registerWebview("tab_detached", 48);
1988 + yield* Effect.yieldNow;
1989 + expect(guest.attach).toHaveBeenCalledTimes(1);
1990 +
1991 + guest.chromiumDetach();
1992 + yield* Effect.yieldNow;
1993 + yield* Effect.yieldNow;
1994 + expect(guest.attach).toHaveBeenCalledTimes(2);
1995 + }),
1996 + ),
1997 + );
1998 +
1999 effectIt.effect("skips a guest destroyed while another guest's session opens", () =>
2000 withManager((manager) =>
2001 Effect.gen(function* () {
apps/desktop/src/preview/Manager.ts
+58 -12
@@ -1023,13 +1023,22 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1023 copy.delete(webContentsId);
1024 }),
1025 ]);
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);
1039 }
1031 - yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
1040 }
1041 + yield* Scope.close(control.scope, Exit.void).pipe(Effect.ignore);
1042 });
1043
1044 const ensureControlSession = Effect.fn("PreviewManager.ensureControlSession")(function* (
@@ -1141,13 +1150,6 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1150 const onMessage: BrowserControlSession["onMessage"] = (_event, method, params) => {
1151 runFork(handleDebuggerMessage(method, params));
1152 };
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 - );
1153 const control: BrowserControlSession = {
1154 webContentsId: wc.id,
1155 debugger: wcDebugger,
@@ -1155,9 +1157,29 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
1157 scope,
1158 onMessage,
1159 };
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 + );
1179 const initialize = Effect.fn("PreviewManager.initializeControlSession")(function* () {
1180 yield* attempt({ operation: "attachDebuggerListeners", webContentsId: wc.id }, () => {
1181 wcDebugger.on("message", onMessage);
1182 + wcDebugger.on("detach", onDetach);
1183 wcDebugger.attach("1.3");
1184 });
1185 // 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
2378 }
2379 }).pipe(Effect.ignore);
2380
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 +
2405 const setColorScheme = Effect.fn("PreviewManager.setColorScheme")(function* (
2406 tabId: string,
2407 colorScheme: DesktopPreviewColorScheme,