Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/tools/builtin/cmd_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build !windows

package builtin

import (
"os"
"syscall"
)

func platformSpecificSysProcAttr() *syscall.SysProcAttr {
return &syscall.SysProcAttr{
Setpgid: true,
}
}

func kill(proc *os.Process) error {
return syscall.Kill(-proc.Pid, syscall.SIGTERM)
}
14 changes: 14 additions & 0 deletions pkg/tools/builtin/cmd_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package builtin

import (
"os"
"syscall"
)

func platformSpecificSysProcAttr() *syscall.SysProcAttr {
return nil
}

func kill(proc *os.Process) error {
return proc.Kill()
}
26 changes: 4 additions & 22 deletions pkg/tools/builtin/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os/exec"
"runtime"
"sync"
"syscall"

"github.com/docker/cagent/pkg/tools"
)
Expand Down Expand Up @@ -54,11 +53,8 @@ func (h *shellHandler) RunShell(ctx context.Context, toolCall tools.ToolCall) (*

// Set up process group for proper cleanup
// On Unix: create new process group so we can kill the entire tree
if runtime.GOOS != "windows" {
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
}
cmd.SysProcAttr = platformSpecificSysProcAttr()

// Note: On Windows, we would set CreationFlags, but that requires
// platform-specific code in a _windows.go file

Expand Down Expand Up @@ -245,22 +241,8 @@ func (t *ShellTool) Stop(context.Context) error {

// Kill all tracked processes
for _, proc := range t.handler.processes {
if proc == nil {
continue
}

// On Unix: kill the entire process group
// On Windows: terminate the process
if runtime.GOOS == "windows" {
// On Windows, we kill the process directly
_ = proc.Kill()
} else {
// On Unix, kill the process group (negative PID kills the group)
// We use SIGTERM first for graceful shutdown
_ = syscall.Kill(-proc.Pid, syscall.SIGTERM)

// Note: We could add a timeout and send SIGKILL if processes don't stop,
// but for now we'll just send SIGTERM
if proc != nil {
_ = kill(proc)
}
}

Expand Down
Loading