@@ -396,6 +396,12 @@ interface PickSession {
396396
397397interface 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