Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

feat: cross-platform process termination for test server

Boschcc89b5dbd8b6
diff --git a/tests/testutil/process_unix.go b/tests/testutil/process_unix.go
new file mode 100644
index 00000000..adce3a80
--- /dev/null
+++ b/tests/testutil/process_unix.go
@@ -0,0 +1,44 @@
+//go:build !windows
+
+package testutil
+
+import (
+ "os"
+ "os/exec"
+ "syscall"
+ "time"
+)
+
+// processGroupAttr returns SysProcAttr that places the child in its own
+// process group, allowing clean shutdown of Chrome children.
+func processGroupAttr() *syscall.SysProcAttr {
+ return &syscall.SysProcAttr{Setpgid: true}
+}
+
+// TerminateProcessGroup sends SIGTERM to the process group, escalating to
+// SIGKILL if the process doesn't exit within the timeout.
+func TerminateProcessGroup(cmd *exec.Cmd, timeout time.Duration) {
+ if cmd.Process == nil {
+ return
+ }
+
+ if pgid, err := syscall.Getpgid(cmd.Process.Pid); err == nil {
+ _ = syscall.Kill(-pgid, syscall.SIGTERM)
+ } else {
+ _ = cmd.Process.Signal(os.Interrupt)
+ }
+
+ done := make(chan error, 1)
+ go func() { done <- cmd.Wait() }()
+
+ select {
+ case <-done:
+ return
+ case <-time.After(timeout):
+ if pgid, err := syscall.Getpgid(cmd.Process.Pid); err == nil {
+ _ = syscall.Kill(-pgid, syscall.SIGKILL)
+ }
+ _ = cmd.Process.Kill()
+ <-done
+ }
+}
diff --git a/tests/testutil/process_windows.go b/tests/testutil/process_windows.go
new file mode 100644
index 00000000..ee46046a
--- /dev/null
+++ b/tests/testutil/process_windows.go
@@ -0,0 +1,40 @@
+//go:build windows
+
+package testutil
+
+import (
+ "fmt"
+ "os/exec"
+ "syscall"
+ "time"
+)
+
+func processGroupAttr() *syscall.SysProcAttr {
+ // CREATE_NEW_PROCESS_GROUP so we can kill the tree on shutdown.
+ return &syscall.SysProcAttr{CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP}
+}
+
+// TerminateProcessGroup kills the process and its children via taskkill /T.
+// Falls back to Process.Kill if taskkill isn't available.
+func TerminateProcessGroup(cmd *exec.Cmd, timeout time.Duration) {
+ if cmd.Process == nil {
+ return
+ }
+
+ // taskkill /T /F /PID kills the process tree
+ kill := exec.Command("taskkill", "/T", "/F", "/PID", // #nosec G204 -- PID is from our own cmd.Process
+ fmt.Sprintf("%d", cmd.Process.Pid))
+ if err := kill.Run(); err != nil {
+ _ = cmd.Process.Kill()
+ }
+
+ done := make(chan error, 1)
+ go func() { done <- cmd.Wait() }()
+
+ select {
+ case <-done:
+ case <-time.After(timeout):
+ _ = cmd.Process.Kill()
+ <-done
+ }
+}
diff --git a/tests/testutil/server.go b/tests/testutil/server.go
index b006672a..8995f1a5 100644
--- a/tests/testutil/server.go
+++ b/tests/testutil/server.go
@@ -11,7 +11,6 @@ import (
"os/exec"
"path/filepath"
"strings"
- "syscall"
"testing"
"time"
)
@@ -109,12 +108,11 @@ func StartServer(cfg ServerConfig) (*Server, error) {
env = append(env, "CHROME_BINARY="+bin)
}
- // Process group enables clean shutdown of Chrome children
s.cmd = exec.Command(s.BinaryPath) // #nosec G204 -- BinaryPath is from os.MkdirTemp, not user input
s.cmd.Env = env
s.cmd.Stdout = os.Stdout
s.cmd.Stderr = os.Stderr
- s.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
+ s.cmd.SysProcAttr = processGroupAttr()
if err := s.cmd.Start(); err != nil {
s.Cleanup()
@@ -149,34 +147,6 @@ func (s *Server) Cleanup() {
_ = os.RemoveAll(s.Dir)
}
-// TerminateProcessGroup sends SIGTERM to the process group, escalating to
-// SIGKILL if the process doesn't exit within the timeout.
-func TerminateProcessGroup(cmd *exec.Cmd, timeout time.Duration) {
- if cmd.Process == nil {
- return
- }
-
- if pgid, err := syscall.Getpgid(cmd.Process.Pid); err == nil {
- _ = syscall.Kill(-pgid, syscall.SIGTERM)
- } else {
- _ = cmd.Process.Signal(os.Interrupt)
- }
-
- done := make(chan error, 1)
- go func() { done <- cmd.Wait() }()
-
- select {
- case <-done:
- return
- case <-time.After(timeout):
- if pgid, err := syscall.Getpgid(cmd.Process.Pid); err == nil {
- _ = syscall.Kill(-pgid, syscall.SIGKILL)
- }
- _ = cmd.Process.Kill()
- <-done
- }
-}
-
func WaitForHealth(base string, timeout time.Duration) bool {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()