fix(exec): terminate process tree on timeout

This commit is contained in:
Luna Reed committed 2026-02-18 02:01:29 +08:00
1 parent ba47892bcf
commit acac1972e6
4 files changed
+148 -2

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
//go:build !windows
package tools
import (
"os/exec"
"syscall"
)
func prepareCommandForTermination(cmd *exec.Cmd) {
if cmd == nil {
return
}
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
func terminateProcessTree(cmd *exec.Cmd) error {
if cmd == nil || cmd.Process == nil {
return nil
}
pid := cmd.Process.Pid
if pid <= 0 {
return nil
}
// Kill the entire process group spawned by the shell command.
_ = syscall.Kill(-pid, syscall.SIGKILL)
// Fallback kill on the shell process itself.
_ = cmd.Process.Kill()
return nil
}