@@ -20,7 +20,6 @@ import * as deepmerge from 'deepmerge';
2020import { spawnWithPipes } from '@testring/child-process' ;
2121import { loggerClient } from '@testring/logger' ;
2222import { getCrxBase64 } from '@testring/dwnld-collector-crx' ;
23- import { CDPCoverageCollector } from '@nullcc/code-coverage-client' ;
2423
2524import type { Cookie } from '@wdio/protocols' ;
2625import type { ClickOptions , MockFilterOptions , WaitUntilOptions } from 'webdriverio' ;
@@ -37,7 +36,6 @@ type browserClientItem = {
3736 client : BrowserObjectCustom ;
3837 sessionId : string ;
3938 initTime : number ;
40- cdpCoverageCollector : CDPCoverageCollector | null ;
4139} ;
4240
4341const DEFAULT_CONFIG : SeleniumPluginConfig = {
@@ -53,7 +51,6 @@ const DEFAULT_CONFIG: SeleniumPluginConfig = {
5351 } ,
5452 'wdio:enforceWebDriverClassic' : true ,
5553 } as any ,
56- cdpCoverage : false ,
5754 disableClientPing : false ,
5855 localVersion : 'v3' as SeleniumVersion ,
5956 seleniumArgs : [ ] ,
@@ -186,6 +183,8 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
186183
187184 private incrementWinId = 0 ;
188185
186+ private killed = false ; // Flag to prevent operations after kill
187+
189188 constructor ( config : Partial < SeleniumPluginConfig > = { } ) {
190189 this . config = this . createConfig ( config ) ;
191190
@@ -246,8 +245,14 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
246245 }
247246
248247 private setupProcessCleanup ( ) {
248+ process . on ( 'exit' , ( ) => this . forceKillSelenium ( ) ) ;
249249 process . on ( 'SIGINT' , ( ) => this . forceKillSelenium ( ) ) ;
250250 process . on ( 'SIGTERM' , ( ) => this . forceKillSelenium ( ) ) ;
251+
252+ // Debug mode specific cleanup handlers
253+ process . on ( 'SIGUSR1' , ( ) => this . forceKillSelenium ( ) ) ; // Debugger disconnect
254+ process . on ( 'SIGUSR2' , ( ) => this . forceKillSelenium ( ) ) ; // Debugger disconnect alternative
255+
251256 // Note: SIGKILL cannot be caught or handled - it immediately terminates the process
252257 }
253258
@@ -302,8 +307,8 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
302307 'standalone' ,
303308 '--port' ,
304309 port . toString ( ) ,
305- '--bind- host' ,
306- 'false ' ,
310+ '--host' ,
311+ '127.0.0.1 ' ,
307312 ) ;
308313 } else {
309314 args . push ( '-jar' , seleniumJarPath , '-port' , port . toString ( ) ) ;
@@ -426,6 +431,10 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
426431 applicant : string ,
427432 config ?: Partial < WebdriverIO . Config > ,
428433 ) : Promise < void > {
434+ if ( this . killed ) {
435+ throw new Error ( 'SeleniumPlugin is being killed' ) ;
436+ }
437+
429438 await this . waitForReadyState ;
430439 const clientData = this . browserClients . get ( applicant ) ;
431440
@@ -463,58 +472,17 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
463472 client as BrowserObjectCustom ,
464473 ) ;
465474
466- let cdpCoverageCollector ;
467- if ( this . config . cdpCoverage ) {
468- this . logger . debug ( 'Started to init cdp coverage....' ) ;
469- cdpCoverageCollector = await this . enableCDPCoverageClient ( client ) ;
470- this . logger . debug ( 'ended to init cdp coverage....' ) ;
471- }
472475 this . browserClients . set ( applicant , {
473476 client : customClient ,
474477 sessionId,
475478 initTime : Date . now ( ) ,
476- cdpCoverageCollector : cdpCoverageCollector
477- ? cdpCoverageCollector
478- : null ,
479479 } ) ;
480480
481481 this . logger . debug (
482482 `Started session for applicant: ${ applicant } . Session id: ${ sessionId } ` ,
483483 ) ;
484484 }
485485
486- private async enableCDPCoverageClient ( client : BrowserObjectCustom ) {
487- if ( this . config . host === undefined ) {
488- return null ;
489- }
490- //accurate
491- if ( ! client . capabilities [ 'se:cdp' ] ) {
492- return null ;
493- }
494- const cdpAddress = client . capabilities [ 'se:cdp' ] ;
495- const collector = new CDPCoverageCollector ( {
496- wsEndpoint : cdpAddress ,
497- } ) ;
498- await collector . init ( ) ;
499- await collector . start ( ) ;
500- return collector ;
501- }
502-
503- public async getCdpCoverageFile ( applicant : string ) {
504- const clientData = this . browserClients . get ( applicant ) ;
505- this . logger . debug ( `start upload coverage for applicant ${ applicant } ` ) ;
506- if ( ! clientData ) {
507- return ;
508- }
509- const coverageCollector = clientData . cdpCoverageCollector ;
510- if ( ! coverageCollector ) {
511- return ;
512- }
513- const { coverage} = await coverageCollector . collect ( ) ;
514- await coverageCollector . stop ( ) ;
515- return [ Buffer . from ( JSON . stringify ( coverage ) ) ] ;
516- }
517-
518486 protected addCustromMethods (
519487 client : BrowserObjectCustom ,
520488 ) : BrowserObjectCustom {
@@ -674,6 +642,9 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
674642
675643 public async kill ( ) {
676644 this . logger . debug ( 'Kill command is called' ) ;
645+
646+ // Set killed flag to prevent new operations
647+ this . killed = true ;
677648
678649 // Close all browser sessions
679650 for ( const applicant of this . browserClients . keys ( ) ) {
@@ -690,6 +661,12 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
690661 }
691662
692663 if ( this . localSelenium ) {
664+ // Check if already killed
665+ if ( this . localSelenium . killed ) {
666+ this . logger . debug ( 'Selenium process already killed' ) ;
667+ return ;
668+ }
669+
693670 // remove listener
694671 if ( this . localSelenium . stderr ) {
695672 this . localSelenium . stderr . removeAllListeners ( 'data' ) ;
@@ -716,7 +693,7 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
716693 } ) ;
717694 } ) ;
718695
719- // Force kill if not exiting within 3 seconds
696+ // Force kill if not exiting within 1 second (reduced from 3 seconds)
720697 const forceKill = new Promise < void > ( ( resolve ) => {
721698 setTimeout ( ( ) => {
722699 if ( this . localSelenium && ! this . localSelenium . killed ) {
@@ -726,7 +703,7 @@ export class SeleniumPlugin implements IBrowserProxyPlugin {
726703 this . localSelenium . kill ( 'SIGKILL' ) ;
727704 }
728705 resolve ( ) ;
729- } , 3000 ) ;
706+ } , 1000 ) ; // Reduced timeout for faster cleanup
730707 } ) ;
731708
732709 // Wait for either normal exit or force kill
0 commit comments