@@ -27,12 +27,14 @@ const child = spawn(
2727) ;
2828
2929let stdout = '' ;
30+ let firstGrandchildPid ;
3031child . stdout . on ( 'data' , ( data ) => {
3132 const dataStr = data . toString ( ) ;
3233 console . log ( `[STDOUT] ${ dataStr } ` ) ;
3334 stdout += `${ dataStr } ` ;
34- if ( / _ _ ( S I G I N T | S I G T E R M ) r e c e i v e d _ _ / . test ( stdout ) ) {
35- console . log ( `[PARENT] Sending kill signal to child process: ${ child . pid } ` ) ;
35+ const match = dataStr . match ( / _ _ ( S I G I N T | S I G T E R M ) r e c e i v e d _ _ ( \d + ) / ) ;
36+ if ( match && match [ 2 ] === firstGrandchildPid ) {
37+ console . log ( `[PARENT] Sending kill signal to watcher process: ${ child . pid } ` ) ;
3638 child . kill ( ) ;
3739 }
3840} ) ;
@@ -44,16 +46,21 @@ child.stdout.on('data', (data) => {
4446// end up in an infinite loop and never receive the stdout of the grandchildren in time.
4547// Only write once to verify the first grandchild process receives the expected signal.
4648// We don't care about the subsequent grandchild processes.
47- let written = false ;
4849child . on ( 'message' , ( msg ) => {
4950 console . log ( `[MESSAGE]` , msg ) ;
50- if ( msg === 'script ready' && ! written ) {
51- writeFileSync ( indexPath , indexContents ) ;
52- written = true ;
51+ if ( ! firstGrandchildPid && typeof msg === 'string' ) {
52+ const match = msg . match ( / s c r i p t r e a d y ( \d + ) / ) ;
53+ if ( match ) {
54+ firstGrandchildPid = match [ 1 ] ; // This is the first grandchild
55+ writeFileSync ( indexPath , indexContents ) ;
56+ }
5357 }
5458} ) ;
5559
5660await once ( child , 'exit' ) ;
5761
58- assert . match ( stdout , / _ _ S I G I N T r e c e i v e d _ _ / ) ;
59- assert . doesNotMatch ( stdout , / _ _ S I G T E R M r e c e i v e d _ _ / ) ;
62+ // The second grandchild, if there is one, could receive SIGTERM if it's killed as a
63+ // consequence of the parent being killed in this process instead of being killed by the
64+ // parent for file changes. Here we only care about the first grandchild.
65+ assert . match ( stdout , new RegExp ( `__SIGINT received__ ${ firstGrandchildPid } ` ) ) ;
66+ assert . doesNotMatch ( stdout , new RegExp ( `__SIGTERM received__ ${ firstGrandchildPid } ` ) ) ;
0 commit comments