@@ -690,19 +690,19 @@ describe("createEmbedPool", () => {
690690
691691 it ( "BUG: shutdown during restart delay still spawns zombie worker" , async ( ) => {
692692 const factory = mockWorkerFactory ( ) ;
693- const pool = createEmbedPool ( factory , { restartDelay : 200 } ) ;
693+ const pool = createEmbedPool ( factory , { restartDelay : 50 } ) ;
694694 pool . initWorker ( ) ;
695695
696- // Worker exits — restart is scheduled with 200ms delay
696+ // Worker exits — restart is scheduled with 50ms delay
697697 factory . workers [ 0 ] . emit ( "exit" , 1 ) ;
698698
699- // Shutdown immediately (before the 200ms restart fires)
699+ // Shutdown immediately (before the 50ms restart fires)
700700 pool . shutdown ( ) ;
701701
702- // Wait for the restart timer to fire
703- await new Promise ( r => setTimeout ( r , 350 ) ) ;
702+ // Wait for the restart timer to fire (guard should catch it)
703+ await new Promise ( r => setTimeout ( r , 150 ) ) ;
704704
705- // Should NOT have created a second worker — shutdown should cancel the restart
705+ // Should NOT have created a second worker — guard prevents it
706706 assert . equal ( factory . workers . length , 1 ,
707707 `Expected 1 worker but got ${ factory . workers . length } — zombie worker spawned after shutdown` ) ;
708708 } ) ;
@@ -904,6 +904,33 @@ describe("createEmbedPool", () => {
904904 assert . equal ( factory . workers . length , 1 , "shutdown should cancel pending restart" ) ;
905905 } ) ;
906906
907+ it ( "belt-and-suspenders: shuttingDown guard catches callback when clearTimeout fails" , async ( ) => {
908+ const factory = mockWorkerFactory ( ) ;
909+ const pool = createEmbedPool ( factory , { restartDelay : 50 } ) ;
910+ pool . initWorker ( ) ;
911+
912+ // Worker exits — restart timer is scheduled
913+ factory . workers [ 0 ] . emit ( "exit" , 1 ) ;
914+
915+ // Monkeypatch clearTimeout to a no-op, simulating a race where
916+ // the timer callback is already queued when shutdown tries to cancel it
917+ const realClearTimeout = globalThis . clearTimeout ;
918+ globalThis . clearTimeout = ( ) => { } ;
919+
920+ try {
921+ pool . shutdown ( ) ;
922+ } finally {
923+ globalThis . clearTimeout = realClearTimeout ;
924+ }
925+
926+ // Timer fires because clearTimeout was neutered — the shuttingDown guard catches it
927+ await new Promise ( r => setTimeout ( r , 150 ) ) ;
928+
929+ // No zombie worker spawned — guard did its job
930+ assert . equal ( factory . workers . length , 1 ,
931+ "shuttingDown guard should prevent zombie worker when clearTimeout fails" ) ;
932+ } ) ;
933+
907934 it ( "uses default options when none provided" , async ( ) => {
908935 const factory = mockWorkerFactory ( ) ;
909936 const pool = createEmbedPool ( factory ) ;
0 commit comments