setProcessGroup gives the shell its own process group and a Cancel that SIGKILLs the whole group. Without it, backgrounded children (`cmd &`) outlive the shell on cancel or timeout, the leak we prevent. Unix-only: Setpgid and negative-PID Kill aren't portable (Windows has its own build).
(cmd *exec.Cmd)
| 12 | // the shell on cancel or timeout, the leak we prevent. Unix-only: Setpgid and |
| 13 | // negative-PID Kill aren't portable (Windows has its own build). |
| 14 | func setProcessGroup(cmd *exec.Cmd) { |
| 15 | cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 16 | cmd.Cancel = func() error { |
| 17 | if cmd.Process == nil { |
| 18 | return nil |
| 19 | } |
| 20 | // Negative pid targets the whole group (shell is the leader). |
| 21 | return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) |
| 22 | } |
| 23 | } |