Wait for subprocess output before releasing resources

This commit is contained in:
2026-12-01 20:20:41 +00:00
parent eb100e54f2
commit da4de023ad
6 changed files with 120 additions and 12 deletions

View File

@@ -2,7 +2,6 @@ package subprocess
import (
"context"
"fmt"
"os/exec"
"time"
)
@@ -80,12 +79,7 @@ func terminateAndReap(tree ownedProcessTree, waitCh <-chan error) (waitErr, clea
case <-timer.C:
}
cleanupErr = joinErrors(cleanupErr, tree.TerminateForcefully())
forcefulTimer := time.NewTimer(forcefulTerminationWait)
defer forcefulTimer.Stop()
select {
case waitErr = <-waitCh:
return waitErr, cleanupErr
case <-forcefulTimer.C:
return nil, joinErrors(cleanupErr, fmt.Errorf("owned subprocess did not reap within %s after forceful termination", forcefulTerminationWait))
}
// Process termination and output copying are not complete until Wait returns.
// Keep ownership of command state and output writers even if I/O is delayed.
return <-waitCh, cleanupErr
}