| 1 |
// Copyright (c) 2025, Daniel Martà <[email protected]> |
| 2 |
// See LICENSE for licensing information |
| 3 |
|
| 4 |
package syntax |
| 5 |
|
| 6 |
import ( |
| 7 |
"os/exec" |
| 8 |
"syscall" |
| 9 |
) |
| 10 |
|
| 11 |
func killCommandOnTestExit(cmd *exec.Cmd) { |
| 12 |
// It's incredibly easy to let an external shell loop forever by accident. |
| 13 |
// In those cases, kill it as soon as the Go test process finishes. |
| 14 |
// |
| 15 |
// Similarly, when killing the shell process due to a timeout, |
| 16 |
// also kill any children processes that the shell spawned via a group. |
| 17 |
cmd.SysProcAttr = &syscall.SysProcAttr{ |
| 18 |
Pdeathsig: syscall.SIGKILL, |
| 19 |
Setpgid: true, |
| 20 |
} |
| 21 |
cmd.Cancel = func() error { |
| 22 |
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) |
| 23 |
} |
| 24 |
} |