Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

platform-specific process handling for unix/windows

Dane Schneidere14c8f9855f8
diff --git a/app/cli/cmd/debug.go b/app/cli/cmd/debug.go
index 3914809b..f9da7912 100644
--- a/app/cli/cmd/debug.go
+++ b/app/cli/cmd/debug.go
@@ -86,7 +86,7 @@ func doDebug(cmd *cobra.Command, args []string) {
execCmd := exec.Command("sh", "-c", shellCmdStr)
execCmd.Dir = cwd
execCmd.Env = os.Environ()
- execCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
+ lib.SetPlatformSpecificAttrs(execCmd)
pipe, err := execCmd.StdoutPipe()
if err != nil {
@@ -121,7 +121,7 @@ func doDebug(cmd *cobra.Command, args []string) {
interrupted.Store(true)
- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGINT); err != nil {
+ if err := lib.KillProcessGroup(execCmd, syscall.SIGINT); err != nil {
log.Printf("Failed to send SIGINT to process group: %v", err)
}
@@ -130,7 +130,7 @@ func doDebug(cmd *cobra.Command, args []string) {
fmt.Println()
color.New(term.ColorHiYellow, color.Bold).Println("πŸ‘‰ Commands didn't exit after 2 seconds. Sending SIGKILL.")
fmt.Println()
- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGKILL); err != nil {
+ if err := lib.KillProcessGroup(execCmd, syscall.SIGKILL); err != nil {
log.Printf("Failed to send SIGKILL to process group: %v", err)
}
case <-ctx.Done():
diff --git a/app/cli/lib/apply.go b/app/cli/lib/apply.go
index f0972125..eb2f0a33 100644
--- a/app/cli/lib/apply.go
+++ b/app/cli/lib/apply.go
@@ -388,11 +388,6 @@ func execApplyScript(
execCmd.Env = os.Environ()
execCmd.Stdin = os.Stdin
- // Set up process group isolation
- execCmd.SysProcAttr = &syscall.SysProcAttr{
- Setpgid: true,
- }
-
// Create a pipe for both stdout and stderr
pipe, err := execCmd.StdoutPipe()
if err != nil {
@@ -401,6 +396,9 @@ func execApplyScript(
}
execCmd.Stderr = execCmd.Stdout
+ // Set platform-specific process attributes
+ SetPlatformSpecificAttrs(execCmd)
+
if err := execCmd.Start(); err != nil {
os.Remove(dstPath)
onErr("failed to start command: %s", err)
@@ -434,8 +432,8 @@ func execApplyScript(
fmt.Println()
interrupted.Store(true)
- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGINT); err != nil {
- log.Printf("Failed to send SIGINT to process group: %v", err)
+ if err := KillProcessGroup(execCmd, syscall.SIGINT); err != nil {
+ log.Printf("Failed to send interrupt signal to process group: %v", err)
}
select {
@@ -443,8 +441,8 @@ func execApplyScript(
fmt.Println()
color.New(term.ColorHiYellow, color.Bold).Println("πŸ‘‰ Commands didn't exit after 2 seconds. Sending SIGKILL.")
fmt.Println()
- if err := syscall.Kill(-execCmd.Process.Pid, syscall.SIGKILL); err != nil {
- log.Printf("Failed to send SIGKILL to process group: %v", err)
+ if err := KillProcessGroup(execCmd, syscall.SIGKILL); err != nil {
+ log.Printf("Failed to terminate process group: %v", err)
}
case <-ctx.Done():
return
diff --git a/app/cli/lib/apply_unix.go b/app/cli/lib/apply_unix.go
new file mode 100644
index 00000000..1b9fb3b4
--- /dev/null
+++ b/app/cli/lib/apply_unix.go
@@ -0,0 +1,18 @@
+//go:build !windows
+
+package lib
+
+import (
+ "os/exec"
+ "syscall"
+)
+
+func SetPlatformSpecificAttrs(cmd *exec.Cmd) {
+ cmd.SysProcAttr = &syscall.SysProcAttr{
+ Setpgid: true,
+ }
+}
+
+func KillProcessGroup(cmd *exec.Cmd, signal syscall.Signal) error {
+ return syscall.Kill(-cmd.Process.Pid, signal)
+}
diff --git a/app/cli/lib/apply_windows.go b/app/cli/lib/apply_windows.go
new file mode 100644
index 00000000..680e5390
--- /dev/null
+++ b/app/cli/lib/apply_windows.go
@@ -0,0 +1,27 @@
+//go:build windows
+
+package lib
+
+import (
+ "os/exec"
+ "strconv"
+ "syscall"
+
+ "golang.org/x/sys/windows"
+)
+
+func SetPlatformSpecificAttrs(cmd *exec.Cmd) {
+ cmd.SysProcAttr = &syscall.SysProcAttr{
+ CreationFlags: windows.CREATE_NEW_PROCESS_GROUP,
+ }
+}
+
+func KillProcessGroup(cmd *exec.Cmd, signal syscall.Signal) error {
+ // Windows uses different signals
+ if signal == syscall.SIGINT {
+ // Ctrl+C event
+ return windows.GenerateConsoleCtrlEvent(windows.CTRL_C_EVENT, uint32(cmd.Process.Pid))
+ }
+ // For SIGKILL equivalent, terminate the process tree
+ return exec.Command("taskkill", "/F", "/T", "/PID", strconv.Itoa(cmd.Process.Pid)).Run()
+}