@@ -85,26 +85,32 @@ function killTree(childProcess: cp.ChildProcessWithoutNullStreams): Promise<void
85
85
return ;
86
86
}
87
87
88
+ const childProcessPid = childProcess . pid ;
89
+ let sawChildProcessPid = false ;
90
+
88
91
const childMap : Record < number , number [ ] > = { } ;
89
92
const pidList = stdout . trim ( ) . split ( / \s + / ) ;
90
93
for ( let i = 0 ; i + 1 < pidList . length ; i += 2 ) {
91
94
const childPid = + pidList [ i ] ;
92
95
const parentPid = + pidList [ i + 1 ] ;
96
+
93
97
childMap [ parentPid ] ||= [ ] ;
94
98
childMap [ parentPid ] . push ( childPid ) ;
99
+
100
+ sawChildProcessPid ||= childPid === childProcessPid ;
95
101
}
96
102
97
- if ( ! childMap [ childProcess . pid ] ) {
103
+ if ( ! sawChildProcessPid ) {
98
104
// Descendent processes may still be alive, but we have no way to identify them
99
105
resolve ( ) ;
100
106
return ;
101
107
}
102
108
103
109
const strictDescendentPids : number [ ] = [ ] ;
104
- const stack : number [ ] = [ childProcess . pid ] ;
110
+ const stack : number [ ] = [ childProcessPid ] ;
105
111
while ( stack . length ) {
106
112
const pid = stack . pop ( ) ! ;
107
- if ( pid !== childProcess . pid ) {
113
+ if ( pid !== childProcessPid ) {
108
114
strictDescendentPids . push ( pid ) ;
109
115
}
110
116
const children = childMap [ pid ] ;
@@ -113,7 +119,7 @@ function killTree(childProcess: cp.ChildProcessWithoutNullStreams): Promise<void
113
119
}
114
120
}
115
121
116
- console . log ( `Killing process ${ childProcess . pid } and its descendents: ${ strictDescendentPids . join ( ", " ) } ` ) ;
122
+ console . log ( `Killing process ${ childProcessPid } and its descendents: ${ strictDescendentPids . join ( ", " ) } ` ) ;
117
123
118
124
strictDescendentPids . forEach ( pid => process . kill ( pid , "SIGKILL" ) ) ;
119
125
childProcess . kill ( "SIGKILL" ) ;
0 commit comments