File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ " @browserbasehq/stagehand " : patch
3
+ ---
4
+
5
+ call this.end() if the process exists
Original file line number Diff line number Diff line change @@ -377,6 +377,7 @@ export class Stagehand {
377
377
private waitForCaptchaSolves : boolean ;
378
378
private localBrowserLaunchOptions ?: LocalBrowserLaunchOptions ;
379
379
public readonly selfHeal : boolean ;
380
+ private cleanupCalled = false ;
380
381
381
382
constructor (
382
383
{
@@ -449,6 +450,30 @@ export class Stagehand {
449
450
450
451
this . selfHeal = selfHeal ;
451
452
this . localBrowserLaunchOptions = localBrowserLaunchOptions ;
453
+
454
+ if ( this . usingAPI ) {
455
+ this . registerSignalHandlers ( ) ;
456
+ }
457
+ }
458
+
459
+ private registerSignalHandlers ( ) {
460
+ const cleanup = async ( signal : string ) => {
461
+ if ( this . cleanupCalled ) return ;
462
+ this . cleanupCalled = true ;
463
+
464
+ console . log ( `[${ signal } ] received. Ending Browserbase session...` ) ;
465
+ try {
466
+ await this . close ( ) ;
467
+ } catch ( err ) {
468
+ console . error ( "Error ending Browserbase session:" , err ) ;
469
+ } finally {
470
+ // Exit explicitly once cleanup is done
471
+ process . exit ( 0 ) ;
472
+ }
473
+ } ;
474
+
475
+ process . once ( "SIGINT" , ( ) => void cleanup ( "SIGINT" ) ) ;
476
+ process . once ( "SIGTERM" , ( ) => void cleanup ( "SIGTERM" ) ) ;
452
477
}
453
478
454
479
public get logger ( ) : ( logLine : LogLine ) => void {
You can’t perform that action at this time.
0 commit comments