@@ -663,57 +663,65 @@ for (const [signal, code] of [
663663 [ "SIGTERM" , 143 ] ,
664664 [ "SIGQUIT" , 131 ] ,
665665] ) {
666- test ( `${ signal } to the wrapper stops the whole tree, releases the lease and exits ${ code } ` , async ( ) => {
667- const dir = freshDir ( ) ;
668- const pidFile = join ( dir , "grandchild.pid" ) ;
669- // The gate stand-in: starts a descendant of its own (as `npm run` does,
670- // four levels deep), records both pids, then idles forever unless
671- // signalled. The descendant is what proves the whole *tree* is stopped —
672- // a regression to `child.kill()` or a non-detached spawn would leave it
673- // running and fail below.
674- const idle =
675- `const d = require("child_process").spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });` +
676- `require("fs").writeFileSync(${ JSON . stringify ( pidFile ) } , process.pid + " " + d.pid);` +
677- `setInterval(() => {}, 1000)` ;
678- const wrapper = spawn (
679- process . execPath ,
680- [ SCRIPT , process . execPath , "-e" , idle ] ,
681- {
682- env : { ...process . env , [ DIR_ENV ] : dir } ,
683- stdio : [ "ignore" , "ignore" , "pipe" ] ,
684- } ,
685- ) ;
686- let stderr = "" ;
687- wrapper . stderr . on ( "data" , ( chunk ) => ( stderr += chunk ) ) ;
688- const exited = new Promise ( ( resolve ) =>
689- wrapper . once ( "exit" , ( code , signal ) => resolve ( { code, signal } ) ) ,
690- ) ;
691-
692- let tree ;
693- try {
694- await waitFor ( ( ) => existsSync ( pidFile ) ) ;
695- tree = readFileSync ( pidFile , "utf8" ) . split ( " " ) . map ( Number ) ;
696- assert . equal ( tree . length , 2 ) ;
697- for ( const pid of tree ) assert . ok ( isAlive ( pid ) , `pid ${ pid } started` ) ;
698- assert . ok ( existsSync ( lockPathOf ( dir ) ) , "the wrapper held the lease" ) ;
699- } finally {
700- // On every path — a failed setup assertion included — the wrapper is
701- // told to stop, so an idle grandchild can never outlive the suite.
702- wrapper . kill ( signal ) ;
703- }
704- const outcome = await exited ;
705- assert . deepEqual ( outcome , { code, signal : null } , stderr ) ;
706- for ( const pid of tree )
707- await waitFor ( ( ) => ! isAlive ( pid ) ) . catch ( ( ) => {
708- process . kill ( pid , "SIGKILL" ) ;
709- assert . fail (
710- `pid ${ pid } outlived the wrapper — the tree was not stopped` ,
711- ) ;
712- } ) ;
713- assert . ok (
714- ! existsSync ( lockPathOf ( dir ) ) ,
715- "the lease was released on the way out" ,
716- ) ;
717- assert . match ( stderr , new RegExp ( `received ${ signal } ; stopping the gate` ) ) ;
718- } ) ;
666+ // POSIX only: Windows has no such signals to send (`kill("SIGQUIT")` throws
667+ // there) and no process groups; its tree is ended with `taskkill` instead,
668+ // which nothing here can exercise without a Windows runner.
669+ const posixOnly = { skip : process . platform === "win32" } ;
670+ test (
671+ `${ signal } to the wrapper stops the whole tree, releases the lease and exits ${ code } ` ,
672+ posixOnly ,
673+ async ( ) => {
674+ const dir = freshDir ( ) ;
675+ const pidFile = join ( dir , "grandchild.pid" ) ;
676+ // The gate stand-in: starts a descendant of its own (as `npm run` does,
677+ // four levels deep), records both pids, then idles forever unless
678+ // signalled. The descendant is what proves the whole *tree* is stopped —
679+ // a regression to `child.kill()` or a non-detached spawn would leave it
680+ // running and fail below.
681+ const idle =
682+ `const d = require("child_process").spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });` +
683+ `require("fs").writeFileSync(${ JSON . stringify ( pidFile ) } , process.pid + " " + d.pid);` +
684+ `setInterval(() => {}, 1000)` ;
685+ const wrapper = spawn (
686+ process . execPath ,
687+ [ SCRIPT , process . execPath , "-e" , idle ] ,
688+ {
689+ env : { ...process . env , [ DIR_ENV ] : dir } ,
690+ stdio : [ "ignore" , "ignore" , "pipe" ] ,
691+ } ,
692+ ) ;
693+ let stderr = "" ;
694+ wrapper . stderr . on ( "data" , ( chunk ) => ( stderr += chunk ) ) ;
695+ const exited = new Promise ( ( resolve ) =>
696+ wrapper . once ( "exit" , ( code , signal ) => resolve ( { code, signal } ) ) ,
697+ ) ;
698+
699+ let tree ;
700+ try {
701+ await waitFor ( ( ) => existsSync ( pidFile ) ) ;
702+ tree = readFileSync ( pidFile , "utf8" ) . split ( " " ) . map ( Number ) ;
703+ assert . equal ( tree . length , 2 ) ;
704+ for ( const pid of tree ) assert . ok ( isAlive ( pid ) , `pid ${ pid } started` ) ;
705+ assert . ok ( existsSync ( lockPathOf ( dir ) ) , "the wrapper held the lease" ) ;
706+ } finally {
707+ // On every path — a failed setup assertion included — the wrapper is
708+ // told to stop, so an idle grandchild can never outlive the suite.
709+ wrapper . kill ( signal ) ;
710+ }
711+ const outcome = await exited ;
712+ assert . deepEqual ( outcome , { code, signal : null } , stderr ) ;
713+ for ( const pid of tree )
714+ await waitFor ( ( ) => ! isAlive ( pid ) ) . catch ( ( ) => {
715+ process . kill ( pid , "SIGKILL" ) ;
716+ assert . fail (
717+ `pid ${ pid } outlived the wrapper — the tree was not stopped` ,
718+ ) ;
719+ } ) ;
720+ assert . ok (
721+ ! existsSync ( lockPathOf ( dir ) ) ,
722+ "the lease was released on the way out" ,
723+ ) ;
724+ assert . match ( stderr , new RegExp ( `received ${ signal } ; stopping the gate` ) ) ;
725+ } ,
726+ ) ;
719727}
0 commit comments