Don't explicitly send interrupt signal to child processes

Apparently the way that process groups work is that child processes will
automatically have all signals propagated to them, so there's no need to
do it explicitly, pmux only needs to wait for them to exit.
This commit is contained in:
Brian Picciano 2022-02-27 11:45:33 -07:00
parent 05c959eba7
commit b4c6787a98
2 changed files with 3 additions and 7 deletions

View File

@ -193,7 +193,7 @@ func main() {
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
sig := <-sigCh sig := <-sigCh
sysLogger.Printf("%v signal received, killing all sub-processes", sig) sysLogger.Printf("%v signal received, waiting for child processes to exit", sig)
cancel() cancel()
<-sigCh <-sigCh

View File

@ -134,16 +134,11 @@ func RunProcessOnce(
return -1, fmt.Errorf("starting process: %w", err) return -1, fmt.Errorf("starting process: %w", err)
} }
// go-routine which will sent interrupt if the context is cancelled. Also
// waits on a secondary channel, which is closed when this function returns,
// in order to ensure this go-routine always gets cleaned up.
stopCh := make(chan struct{}) stopCh := make(chan struct{})
defer close(stopCh)
go func(proc *os.Process) { go func(proc *os.Process) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
_ = proc.Signal(os.Interrupt)
case <-stopCh: case <-stopCh:
return return
} }
@ -153,13 +148,14 @@ func RunProcessOnce(
sysLogger.Println("forcefully killing process") sysLogger.Println("forcefully killing process")
_ = proc.Signal(os.Kill) _ = proc.Signal(os.Kill)
case <-stopCh: case <-stopCh:
return
} }
}(cmd.Process) }(cmd.Process)
wg.Wait() wg.Wait()
err = cmd.Wait() err = cmd.Wait()
close(stopCh)
exitCode := cmd.ProcessState.ExitCode() exitCode := cmd.ProcessState.ExitCode()
if err != nil { if err != nil {