@@ -144,7 +144,6 @@ export async function runManagedCommand({
144144 let timeoutTimer = null ;
145145 let signalTimeout ;
146146 let timedOut = false ;
147- let childResult ;
148147 let timeoutTermination ;
149148 const timeoutTriggered = new Promise ( ( resolve ) => {
150149 signalTimeout = resolve ;
@@ -222,14 +221,10 @@ export async function runManagedCommand({
222221 }
223222 throw outcome . error ;
224223 }
225- childResult = outcome . status ;
226224 if ( requireProcessTreeExit ) {
227- await ensureManagedProcessTreeExit ( child , platform , {
228- rejectIfLive : true ,
229- terminateIfLive : true ,
230- } ) ;
225+ await ensureManagedProcessTreeExit ( child , platform , { terminateIfLive : true } ) ;
231226 }
232- return childResult ;
227+ return outcome . status ;
233228 } finally {
234229 if ( timeoutTimer ) {
235230 clearTimeout ( timeoutTimer ) ;
@@ -276,7 +271,7 @@ function processGroupStatus(pid) {
276271async function ensureManagedProcessTreeExit (
277272 child ,
278273 platform ,
279- { rejectIfLive = false , terminateIfLive = false , windowsTermination } = { } ,
274+ { terminateIfLive = false , windowsTermination } = { } ,
280275) {
281276 if ( platform === "win32" ) {
282277 if ( windowsTermination ?. processTreeState === "indeterminate" ) {
@@ -294,18 +289,18 @@ async function ensureManagedProcessTreeExit(
294289 return ;
295290 }
296291 let status = initialStatus ;
297- let sawLive = initialStatus === "live" ;
298- if ( terminateIfLive ) {
299- terminateManagedChild ( child , "SIGKILL" , { platform } ) ;
300- }
292+ // A missing group at signal time supersedes the earlier racy liveness probe.
293+ const termination = terminateIfLive
294+ ? terminateManagedChild ( child , "SIGKILL" , { platform } )
295+ : undefined ;
301296 const deadline = Date . now ( ) + PROCESS_GROUP_DRAIN_TIMEOUT_MS ;
302297 while ( Date . now ( ) < deadline ) {
303298 await new Promise ( ( resolve ) => {
304299 setTimeout ( resolve , PROCESS_GROUP_POLL_MS ) ;
305300 } ) ;
306301 status = processGroupStatus ( child . pid ) ;
307302 if ( status === "dead" ) {
308- if ( rejectIfLive && sawLive ) {
303+ if ( terminateIfLive && termination ?. processTreeState !== "terminated" ) {
309304 throw createManagedCommandCleanupError (
310305 "Managed command exited while its process group remained active" ,
311306 child ,
@@ -315,9 +310,6 @@ async function ensureManagedProcessTreeExit(
315310 }
316311 return ;
317312 }
318- if ( status === "live" ) {
319- sawLive = true ;
320- }
321313 }
322314 const processTreeState = status === "indeterminate" ? "indeterminate" : "live" ;
323315 throw createManagedCommandCleanupError (
0 commit comments