fix(desktop): preview CDP sessions no longer hard-crash the app #9068
main fromrefs/pull/9068/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubCloses #9067.
The desktop app was hard-crashing (SIGSEGV in the browser process) after heavy preview automation — three crashes in one day, across two nightlies and two Electron versions. Symbolicated dumps showed Chromium's DevTools layer dispatching into a freed DevToolsAgentHostClient: Electron's webContents.debugger wrapper is cppgc-managed but registered with content as a raw pointer, and its destructor never detaches, so a GC sweep of an attached debugger leaves the session dangling (reported upstream as electron/electron#53376). Our exposure: nothing pinned the wrapper for the control session's lifetime, and the cleanup finalizer re-read wc.debugger — which throws once the webview is destroyed — so detach was silently skipped exactly when it mattered.
The fix pins the debugger reference in BrowserControlSession and routes every attach, detach, and sendCommand through it:
- While a control session exists, the wrapper is strongly referenced, so Chromium's force-detach during WebContents teardown always finds a live client.
- The scope finalizer detaches through the pinned reference, which stays valid after the WebContents wrapper is destroyed (
Debugger.detach()is a safe no-op if the agent host is already closed).
New test drives the real-world shape the old mocks hid: a webview whose debugger getter throws after destruction. It fails on the previous code (detach never called) and passes with the fix. vp test run apps/desktop/src/preview/Manager.test.ts (64 passed), desktop tsgo --noEmit and lint clean.
Fable 5 via Claude Code.
Note
High Risk Touches CDP attach/detach lifecycle for preview automation; incorrect pinning or teardown could regress crashes or leave debugger sessions attached.
Overview
Fixes browser-process crashes during heavy preview automation by keeping the Electron Debugger wrapper alive for each preview control session and routing all CDP traffic through that pinned handle instead of re-reading webContents.debugger.
BrowserControlSession now stores the debugger captured at attach time. Session setup, teardown (off / detach), automation sendCommand, and color-scheme emulation all use control.debugger, so cleanup still runs when the webview is already destroyed (when wc.debugger would throw per electron/electron#53376).
Adds a test where the webview’s debugger getter throws after destruction and asserts detach still runs on navigate/cleanup.
Note
Pin Electron.Debugger reference in BrowserControlSession to prevent preview CDP crashes#
- Captures
wc.debuggerinto a localwcDebuggerat control session creation in Manager.ts, so all attach, detach, message listener, andsendCommandcalls use the pinned reference instead of re-accessingwc.debuggerat runtime - Adds a readonly
debuggerfield toBrowserControlSessionand exposes the pinned instance on it, socontrolAction,applyColorScheme, andrestoreControlSessionall route throughcontrol.debugger - Adds an effect test in Manager.test.ts that marks the WebContents as destroyed, then asserts
debugger.off('message', ...)anddebugger.detach()still run via the pinned reference - Risk: any code outside
PreviewManagerthat still readswc.debuggerdirectly on a destroyed WebContents will still crash; only the in-tree session paths are hardened
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
Approvability#
Verdict: Approved at
6441d6dMacroscope's review found this PR approvable — This is a focused desktop bug fix that pins the existing Electron debugger wrapper for each control session and routes existing commands and cleanup through it, preventing crash-prone garbage collection and destroyed-WebContents cleanup failures. A targeted regression test covers the affected lifecycle, with no product-default or static-analysis changes.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes