when stopping hooks, stop all their subprocesses too (#3004) (#3087)

This commit is contained in:
Alessandro Ros
2024-02-29 22:42:11 +01:00
committed by GitHub
parent a40ca33300
commit 44918fce0d
4 changed files with 73 additions and 23 deletions

View File

@@ -13,7 +13,7 @@ import (
"github.com/kballard/go-shellquote"
)
func (e *Cmd) runOSSpecific() error {
func (e *Cmd) runOSSpecific(env []string) error {
cmdParts, err := shellquote.Split(e.cmdstr)
if err != nil {
return err
@@ -21,14 +21,13 @@ func (e *Cmd) runOSSpecific() error {
cmd := exec.Command(cmdParts[0], cmdParts[1:]...)
cmd.Env = append([]string(nil), os.Environ()...)
for key, val := range e.env {
cmd.Env = append(cmd.Env, key+"="+val)
}
cmd.Env = env
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// set process group in order to allow killing subprocesses
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err = cmd.Start()
if err != nil {
return err
@@ -51,7 +50,8 @@ func (e *Cmd) runOSSpecific() error {
select {
case <-e.terminate:
syscall.Kill(cmd.Process.Pid, syscall.SIGINT) //nolint:errcheck
// the minus is needed to kill all subprocesses
syscall.Kill(-cmd.Process.Pid, syscall.SIGINT) //nolint:errcheck
<-cmdDone
return errTerminated