@@ -69,7 +69,7 @@ import {
6969 SessionContextType ,
7070 categorizeError ,
7171} from "./errors/launch-errors.js" ;
72- import { BasePlugin } from "./plugins/core/base-plugin.js" ;
72+ import { BasePlugin , ShutdownReason } from "./plugins/core/base-plugin.js" ;
7373import { PluginManager } from "./plugins/core/plugin-manager.js" ;
7474import { isSimilarConfig , validateLaunchConfig , validateTimezone } from "./utils/validation.js" ;
7575import { TargetInstrumentationManager } from "./instrumentation/target-manager.js" ;
@@ -376,7 +376,7 @@ export class CDPService extends EventEmitter {
376376 `[CDPService] Blocked response from file protocol: ${ response . url ( ) } ` ,
377377 ) ;
378378 page . close ( ) . catch ( ( ) => { } ) ;
379- this . shutdown ( ) ;
379+ this . endSession ( ShutdownReason . SECURITY_VIOLATION ) ;
380380 }
381381 } ) ;
382382 }
@@ -436,7 +436,7 @@ export class CDPService extends EventEmitter {
436436 if ( url . startsWith ( "file://" ) ) {
437437 this . logger . error ( `[CDPService] Blocked request to file protocol: ${ url } ` ) ;
438438 page . close ( ) . catch ( ( ) => { } ) ;
439- this . shutdown ( ) ;
439+ this . endSession ( ShutdownReason . SECURITY_VIOLATION ) ;
440440 } else {
441441 await request . continue ( { headers } ) ;
442442 }
@@ -456,16 +456,16 @@ export class CDPService extends EventEmitter {
456456 }
457457
458458 @traceable
459- public async shutdown ( ) : Promise < void > {
459+ public async shutdown ( reason : ShutdownReason ) : Promise < void > {
460460 this . shuttingDown = true ;
461- this . logger . info ( `[CDPService] Shutting down and cleaning up resources` ) ;
461+ this . logger . info ( `[CDPService] Shutting down and cleaning up resources (reason: ${ reason } ) ` ) ;
462462
463463 try {
464464 if ( this . browserInstance ) {
465465 await this . pluginManager . onBrowserClose ( this . browserInstance ) ;
466466 }
467467
468- await this . pluginManager . onShutdown ( ) ;
468+ await this . pluginManager . onShutdown ( reason ) ;
469469
470470 this . removeAllHandlers ( ) ;
471471 await this . browserInstance ?. close ( ) ;
@@ -527,7 +527,7 @@ export class CDPService extends EventEmitter {
527527 return await this . launchInternal ( config ) ;
528528 } catch ( error ) {
529529 try {
530- await this . pluginManager . onShutdown ( ) ;
530+ await this . pluginManager . onShutdown ( ShutdownReason . LAUNCH_FAILURE ) ;
531531 await this . shutdownHook ( ) ;
532532 } catch ( e ) {
533533 this . logger . warn (
@@ -609,7 +609,7 @@ export class CDPService extends EventEmitter {
609609 ) ;
610610 await executeBestEffort (
611611 this . logger ,
612- async ( ) => this . shutdown ( ) ,
612+ async ( ) => this . shutdown ( ShutdownReason . RELAUNCH ) ,
613613 "Error during shutdown before launch" ,
614614 ) ;
615615 }
@@ -1278,21 +1278,24 @@ export class CDPService extends EventEmitter {
12781278 try {
12791279 return await this . launch ( sessionConfig ) ;
12801280 } catch ( error ) {
1281+ // If launch fails, ensure we still notify plugins about session end to allow for proper cleanup
1282+ await this . pluginManager . onBeforeSessionEnd ( sessionConfig ) ;
1283+ await this . pluginManager . onSessionEnd ( sessionConfig ) ;
12811284 await this . pluginManager . onAfterSessionEnd ( sessionConfig ) ;
12821285 throw error ;
12831286 }
12841287 }
12851288
12861289 @traceable
1287- public async endSession ( ) : Promise < void > {
1290+ public async endSession ( reason : ShutdownReason = ShutdownReason . SESSION_END ) : Promise < void > {
12881291 this . logger . info ( "Ending current session and resetting to default configuration." ) ;
12891292 const sessionConfig = this . currentSessionConfig ! ;
12901293
12911294 this . sessionContext = await this . getBrowserState ( ) . catch ( ( ) => null ) ;
12921295
12931296 try {
12941297 await this . pluginManager . onBeforeSessionEnd ( sessionConfig ) ;
1295- await this . shutdown ( ) ;
1298+ await this . shutdown ( reason ) ;
12961299 await this . pluginManager . onSessionEnd ( sessionConfig ) ;
12971300 this . currentSessionConfig = null ;
12981301 this . sessionContext = null ;
0 commit comments