Skip to content

Commit 6441d6d

Browse files
sethwebsterclaude
andcommitted
fix(desktop): pin the preview CDP debugger so GC cannot crash the app
The control session's wc.debugger wrapper is cppgc-managed in Electron but registered with Chromium as a raw DevToolsAgentHostClient pointer, and its destructor never detaches (electron/electron#53376). Once nothing referenced the wrapper, a GC sweep left the DevTools session dangling and the browser process segfaulted on the next CDP notification or WebContents teardown. Cleanup also read wc.debugger again, which throws after the webview is destroyed, silently skipping detach. Hold the debugger reference in BrowserControlSession for the session's lifetime and run every attach/detach/sendCommand through it. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent f0fb83a commit 6441d6d

2 files changed

Lines changed: 81 additions & 12 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,67 @@ describe("PreviewManager", () => {
533533
),
534534
);
535535

536+
effectIt.effect("detaches through the pinned debugger after the webview is destroyed", () =>
537+
withManager((manager) =>
538+
Effect.gen(function* () {
539+
// Real Electron throws on any `wc.debugger` access once the
540+
// WebContents is destroyed, so cleanup must go through the debugger
541+
// reference captured at attach time (electron/electron#53376).
542+
let destroyed = false;
543+
let attached = false;
544+
const debuggerOff = vi.fn();
545+
const debuggerDetach = vi.fn(() => {
546+
attached = false;
547+
});
548+
const wcDebugger = {
549+
isAttached: () => attached,
550+
attach: vi.fn(() => {
551+
attached = true;
552+
}),
553+
detach: debuggerDetach,
554+
sendCommand: vi.fn(async () => undefined),
555+
on: vi.fn(),
556+
off: debuggerOff,
557+
};
558+
fromId.mockReturnValue({
559+
id: 42,
560+
isDestroyed: () => destroyed,
561+
getType: () => "webview",
562+
getURL: () => "http://localhost:3200/",
563+
getTitle: () => "Preview",
564+
isLoading: () => false,
565+
isDevToolsOpened: () => false,
566+
getZoomFactor: () => 1,
567+
setZoomFactor: vi.fn(),
568+
setAudioMuted: vi.fn(),
569+
isCurrentlyAudible: () => false,
570+
reload: vi.fn(),
571+
loadURL: vi.fn(async () => undefined),
572+
on: vi.fn(),
573+
off: vi.fn(),
574+
ipc: { on: vi.fn(), off: vi.fn() },
575+
send: webviewSend,
576+
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
577+
setWindowOpenHandler: vi.fn(),
578+
get debugger() {
579+
if (destroyed) throw new Error("Object has been destroyed");
580+
return wcDebugger;
581+
},
582+
} as never);
583+
yield* manager.createTab("tab_pinned_debugger");
584+
yield* manager.registerWebview("tab_pinned_debugger", 42);
585+
yield* manager.setColorScheme("tab_pinned_debugger", "dark");
586+
expect(attached).toBe(true);
587+
destroyed = true;
588+
589+
yield* manager.navigate("tab_pinned_debugger", "https://example.com/");
590+
591+
expect(debuggerOff).toHaveBeenCalledWith("message", expect.any(Function));
592+
expect(debuggerDetach).toHaveBeenCalledOnce();
593+
}),
594+
),
595+
);
596+
536597
effectIt.effect("does not let destroyed-webview cleanup detach a same-id replacement", () =>
537598
withManager((manager) =>
538599
Effect.gen(function* () {

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ interface PickSession {
396396

397397
interface BrowserControlSession {
398398
readonly webContentsId: number;
399+
// Pins the WebContents' Debugger wrapper for the session's lifetime.
400+
// Electron's Debugger is GC-managed but registered with Chromium as a raw
401+
// DevToolsAgentHostClient pointer; collecting it while attached crashes the
402+
// browser process (electron/electron#53376). Detach must also go through
403+
// this reference: `wc.debugger` throws once the WebContents is destroyed.
404+
readonly debugger: Electron.Debugger;
399405
readonly semaphore: Semaphore.Semaphore;
400406
readonly scope: Scope.Closeable;
401407
readonly onMessage: (
@@ -1014,6 +1020,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
10141020
const createControlSession = Effect.fn("PreviewManager.createControlSession")(function* () {
10151021
const semaphore = yield* Semaphore.make(1);
10161022
const scope = yield* Scope.fork(parentScope, "sequential");
1023+
const wcDebugger = wc.debugger;
10171024
const handleDebuggerMessage = Effect.fnUntraced(function* (
10181025
method: string,
10191026
params: Record<string, unknown>,
@@ -1026,7 +1033,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
10261033
operation: "ackScreencastFrame",
10271034
webContentsId: wc.id,
10281035
},
1029-
() => wc.debugger.sendCommand("Page.screencastFrameAck", { sessionId }),
1036+
() => wcDebugger.sendCommand("Page.screencastFrameAck", { sessionId }),
10301037
).pipe(Effect.ignore);
10311038
}
10321039
const tabId = yield* tabIdForWebContents(wc.id);
@@ -1074,15 +1081,16 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
10741081
}),
10751082
),
10761083
attempt({ operation: "detachControlSession", webContentsId: wc.id }, () => {
1077-
wc.debugger.off("message", onMessage);
1078-
if (wc.debugger.isAttached()) wc.debugger.detach();
1084+
wcDebugger.off("message", onMessage);
1085+
if (wcDebugger.isAttached()) wcDebugger.detach();
10791086
}).pipe(Effect.ignore),
10801087
],
10811088
{ discard: true },
10821089
),
10831090
);
10841091
const control: BrowserControlSession = {
10851092
webContentsId: wc.id,
1093+
debugger: wcDebugger,
10861094
semaphore,
10871095
scope,
10881096
onMessage,
@@ -1098,15 +1106,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
10981106
}),
10991107
);
11001108
yield* attempt({ operation: "attachDebuggerListeners", webContentsId: wc.id }, () => {
1101-
wc.debugger.on("message", onMessage);
1102-
wc.debugger.attach("1.3");
1109+
wcDebugger.on("message", onMessage);
1110+
wcDebugger.attach("1.3");
11031111
});
11041112
yield* Effect.all(
11051113
["Runtime.enable", "Accessibility.enable", "Network.enable", "Log.enable"].map(
11061114
(method) =>
11071115
attemptPromise(
11081116
{ operation: `initializeDebugger.${method}`, webContentsId: wc.id },
1109-
() => wc.debugger.sendCommand(method),
1117+
() => wcDebugger.sendCommand(method),
11101118
),
11111119
),
11121120
{ concurrency: "unbounded", discard: true },
@@ -1195,7 +1203,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
11951203
}
11961204
const result = yield* attemptPromise(
11971205
{ operation: `${action}.${method}`, tabId, webContentsId: wc.id },
1198-
() => wc.debugger.sendCommand(method, commandParams),
1206+
() => control.debugger.sendCommand(method, commandParams),
11991207
);
12001208
const after = (yield* Ref.get(controlEpochRef)).get(tabId) ?? 0;
12011209
if (after !== epoch) {
@@ -1219,7 +1227,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
12191227
tabId,
12201228
webContentsId: wc.id,
12211229
},
1222-
() => wc.debugger.sendCommand(method, commandParams),
1230+
() => control.debugger.sendCommand(method, commandParams),
12231231
);
12241232
},
12251233
);
@@ -2348,9 +2356,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
23482356
wc: Electron.WebContents,
23492357
colorScheme: DesktopPreviewColorScheme,
23502358
) {
2351-
yield* ensureControlSession(wc);
2359+
const control = yield* ensureControlSession(wc);
23522360
yield* attemptPromise({ operation: "applyColorScheme", tabId, webContentsId: wc.id }, () =>
2353-
wc.debugger.sendCommand("Emulation.setEmulatedMedia", {
2361+
control.debugger.sendCommand("Emulation.setEmulatedMedia", {
23542362
features: [
23552363
{
23562364
name: "prefers-color-scheme",
@@ -2370,15 +2378,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
23702378
Effect.gen(function* () {
23712379
const beforeAttach = (yield* SynchronizedRef.get(tabsRef)).get(tabId);
23722380
if (beforeAttach?.webContentsId !== wc.id) return;
2373-
yield* ensureControlSession(wc);
2381+
const control = yield* ensureControlSession(wc);
23742382
const afterAttach = (yield* SynchronizedRef.get(tabsRef)).get(tabId);
23752383
if (afterAttach?.webContentsId !== wc.id) {
23762384
yield* detachControlSession(wc.id);
23772385
return;
23782386
}
23792387
if (afterAttach.colorScheme !== "system") {
23802388
yield* attemptPromise({ operation: "applyColorScheme", tabId, webContentsId: wc.id }, () =>
2381-
wc.debugger.sendCommand("Emulation.setEmulatedMedia", {
2389+
control.debugger.sendCommand("Emulation.setEmulatedMedia", {
23822390
features: [
23832391
{
23842392
name: "prefers-color-scheme",

0 commit comments

Comments
 (0)