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
22 changes: 19 additions & 3 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@ var errProviderCommandTimeout = errors.New("provider command timeout")

func runProviderCommand(command string, timeout time.Duration) ([]byte, []byte, error) {
cmd := shellCommand(command)
configureCommandProcess(cmd)
// Bound the time Wait spends draining I/O pipes after the process exits,
// in case an orphaned descendant still holds the write ends open.
cmd.WaitDelay = time.Second

var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Start(); err != nil {
proc, err := startCommandProcess(cmd)
if err != nil {
return nil, nil, err
}
defer proc.Close()

done := make(chan error, 1)
go func() {
Expand All @@ -63,9 +67,21 @@ func runProviderCommand(command string, timeout time.Duration) ([]byte, []byte,

select {
case err := <-done:
if err != nil {
// The shell exited (whether via ErrWaitDelay because a
// background descendant kept the inherited stdout/stderr pipes
// open, or via a nonzero exit status) but a leftover descendant
// may still be running. Terminate is a no-op against an
// already-dead tree, so always call it rather than gating on
// the specific error to avoid leaking that descendant.
proc.Terminate()
}
if errors.Is(err, exec.ErrWaitDelay) {
return stdout.Bytes(), stderr.Bytes(), errProviderCommandTimeout
}
return stdout.Bytes(), stderr.Bytes(), err
case <-timer.C:
terminateCommandProcess(cmd)
proc.Terminate()
<-done
return stdout.Bytes(), stderr.Bytes(), errProviderCommandTimeout
}
Expand Down
128 changes: 126 additions & 2 deletions internal/config/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -67,7 +68,8 @@ func TestLoadProviderCommandFailureIncludesExitAndRedactsOutput(t *testing.T) {
}

func TestLoadProviderCommandTimeout(t *testing.T) {
command := writeCommand(t, commandScript{SleepSeconds: 10})
pidFile := filepath.Join(t.TempDir(), "sleep.pid")
command := writeCommand(t, commandScript{SleepSeconds: 10, PidFile: pidFile})

start := time.Now()
_, err := LoadProviderCommand(command)
Expand All @@ -85,6 +87,87 @@ func TestLoadProviderCommandTimeout(t *testing.T) {
if elapsed > maxElapsed {
t.Fatalf("timeout returned after %s, want roughly 5s", elapsed)
}
assertProcessTerminated(t, pidFile)
}

// TestLoadProviderCommandTerminatesBackgroundChild covers a command that
// exits immediately but leaves a detached child holding the inherited
// stdout/stderr pipes open (e.g. `sleep 600 & exit`). cmd.Wait() only
// unblocks once WaitDelay elapses, and that path must still terminate the
// leftover child instead of just returning and leaking it.
func TestLoadProviderCommandTerminatesBackgroundChild(t *testing.T) {
pidFile := filepath.Join(t.TempDir(), "bg.pid")
command := writeCommand(t, commandScript{
Stdout: `{"name":"cmd","provider":"openai","apiKey":"sk-command","model":"gpt-command"}`,
BackgroundSleepSeconds: 10,
BackgroundPidFile: pidFile,
})

start := time.Now()
_, err := LoadProviderCommand(command)
elapsed := time.Since(start)
if err == nil {
t.Fatal("LoadProviderCommand() error = nil, want error from WaitDelay-bounded wait")
}
if !strings.Contains(err.Error(), "timed out after 5s") {
t.Fatalf("error = %q, want timeout", err.Error())
}
if elapsed > 4*time.Second {
t.Fatalf("returned after %s, want well under the 5s provider-command timeout since WaitDelay (1s) should trigger termination", elapsed)
}
assertProcessTerminated(t, pidFile)
}

// TestLoadProviderCommandTerminatesBackgroundChildOnFailure covers the same
// leaked-descendant scenario as TestLoadProviderCommandTerminatesBackgroundChild,
// but with a shell that exits nonzero. Go's exec.Cmd.Wait only returns
// exec.ErrWaitDelay when the command itself exited successfully; a nonzero
// exit yields the bare *ExitError instead, so the leftover child must still
// be terminated on that path.
func TestLoadProviderCommandTerminatesBackgroundChildOnFailure(t *testing.T) {
pidFile := filepath.Join(t.TempDir(), "bg-fail.pid")
command := writeCommand(t, commandScript{
Stderr: "boom",
ExitCode: 7,
BackgroundSleepSeconds: 10,
BackgroundPidFile: pidFile,
})

start := time.Now()
_, err := LoadProviderCommand(command)
elapsed := time.Since(start)
if err == nil {
t.Fatal("LoadProviderCommand() error = nil, want failure from nonzero exit")
}
if !strings.Contains(err.Error(), "exit status") {
t.Fatalf("error = %q, want exit status failure", err.Error())
}
if elapsed > 4*time.Second {
t.Fatalf("returned after %s, want well under the 5s provider-command timeout since WaitDelay (1s) should trigger termination", elapsed)
}
assertProcessTerminated(t, pidFile)
}

func assertProcessTerminated(t *testing.T, pidFile string) {
t.Helper()

data, err := os.ReadFile(pidFile)
if err != nil {
t.Fatalf("read sleeper pid file: %v", err)
}
pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
if err != nil {
t.Fatalf("parse sleeper pid %q: %v", data, err)
}

deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
if !processAlive(pid) {
return
}
time.Sleep(100 * time.Millisecond)
}
t.Fatalf("sleeper process %d still alive after timeout", pid)
}

func TestLoadProviderCommandInvalidJSON(t *testing.T) {
Expand Down Expand Up @@ -118,6 +201,14 @@ type commandScript struct {
Stderr string
ExitCode int
SleepSeconds int
PidFile string

// BackgroundSleepSeconds, if set, spawns a detached child that keeps the
// inherited stdout/stderr handles open well after the script itself
// exits, simulating a `sleep 600 & exit` style command. BackgroundPidFile
// records the detached child's PID.
BackgroundSleepSeconds int
BackgroundPidFile string
}

func writeCommand(t *testing.T, script commandScript) string {
Expand All @@ -128,14 +219,27 @@ func writeCommand(t *testing.T, script commandScript) string {
path := filepath.Join(dir, "provider.cmd")
lines := []string{"@echo off"}
if script.SleepSeconds > 0 {
lines = append(lines, "powershell -NoProfile -Command \"Start-Sleep -Seconds "+itoa(script.SleepSeconds)+"\"")
sleep := "Start-Sleep -Seconds " + itoa(script.SleepSeconds)
if script.PidFile != "" {
sleep = "Set-Content -Path '" + psSingleQuote(script.PidFile) + "' -Value $PID -Encoding Ascii; " + sleep
}
lines = append(lines, "powershell -NoProfile -Command \""+sleep+"\"")
}
if script.Stdout != "" {
lines = append(lines, "echo "+script.Stdout)
}
if script.Stderr != "" {
lines = append(lines, "echo "+script.Stderr+" 1>&2")
}
if script.BackgroundSleepSeconds > 0 {
readyFile := script.BackgroundPidFile + ".ready"
bgSleep := "Set-Content -LiteralPath '" + psSingleQuote(script.BackgroundPidFile) + "' -Value $PID -Encoding Ascii; " +
"Set-Content -LiteralPath '" + psSingleQuote(readyFile) + "' -Value ready -Encoding Ascii; " +
"Start-Sleep -Seconds " + itoa(script.BackgroundSleepSeconds)
lines = append(lines, "start /B powershell -NoProfile -Command \""+bgSleep+"\"")
waitForReady := "while (-not (Test-Path -LiteralPath '" + psSingleQuote(readyFile) + "')) { Start-Sleep -Milliseconds 10 }"
lines = append(lines, "powershell -NoProfile -Command \""+waitForReady+"\"")
}
lines = append(lines, "exit /b "+itoa(script.ExitCode))
if err := os.WriteFile(path, []byte(strings.Join(lines, "\r\n")), 0o700); err != nil {
t.Fatalf("write command: %v", err)
Expand All @@ -146,6 +250,9 @@ func writeCommand(t *testing.T, script commandScript) string {
path := filepath.Join(dir, "provider.sh")
lines := []string{"#!/bin/sh"}
if script.SleepSeconds > 0 {
if script.PidFile != "" {
lines = append(lines, "echo $$ > '"+shSingleQuote(script.PidFile)+"'")
}
lines = append(lines, "sleep "+itoa(script.SleepSeconds))
}
if script.Stdout != "" {
Expand All @@ -154,13 +261,30 @@ func writeCommand(t *testing.T, script commandScript) string {
if script.Stderr != "" {
lines = append(lines, "printf '%s\\n' '"+script.Stderr+"' >&2")
}
if script.BackgroundSleepSeconds > 0 {
lines = append(lines, "sleep "+itoa(script.BackgroundSleepSeconds)+" &")
lines = append(lines, "echo $! > '"+shSingleQuote(script.BackgroundPidFile)+"'")
}
lines = append(lines, "exit "+itoa(script.ExitCode))
if err := os.WriteFile(path, []byte(strings.Join(lines, "\n")), 0o700); err != nil {
t.Fatalf("write command: %v", err)
}
return path
}

// psSingleQuote escapes a value for interpolation inside a PowerShell
// single-quoted string literal, where a literal quote is doubled.
func psSingleQuote(value string) string {
return strings.ReplaceAll(value, "'", "''")
}

// shSingleQuote escapes a value for interpolation inside a POSIX shell
// single-quoted string literal, where a literal quote must close the
// quoted section, emit an escaped quote, then reopen it.
func shSingleQuote(value string) string {
return strings.ReplaceAll(value, "'", `'\''`)
}

func itoa(value int) string {
if value == 0 {
return "0"
Expand Down
55 changes: 48 additions & 7 deletions internal/config/process_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,59 @@
package config

import (
"io"
"os/exec"
"syscall"
)

func configureCommandProcess(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
type commandProcess struct {
groupID int
anchor *exec.Cmd
anchorInput io.WriteCloser
}

func terminateCommandProcess(cmd *exec.Cmd) {
if cmd.Process == nil {
return
// startCommandProcess retains a live process-group leader until cleanup. The
// provider shell may be reaped before Wait finishes draining descendant-held
// pipes, so its PID cannot safely identify the group after Wait returns.
func startCommandProcess(cmd *exec.Cmd) (*commandProcess, error) {
anchor := exec.Command("sh", "-c", "read _")
anchor.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
anchorInput, err := anchor.StdinPipe()
if err != nil {
return nil, err
}
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
_ = cmd.Process.Kill()
if err := anchor.Start(); err != nil {
_ = anchorInput.Close()
return nil, err
}

proc := &commandProcess{groupID: anchor.Process.Pid, anchor: anchor, anchorInput: anchorInput}
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
cmd.SysProcAttr.Setpgid = true
cmd.SysProcAttr.Pgid = proc.groupID
if err := cmd.Start(); err != nil {
proc.Close()
return nil, err
}
return proc, nil
}

func (p *commandProcess) Terminate() {
if p.groupID != 0 {
_ = syscall.Kill(-p.groupID, syscall.SIGKILL)
}
}

func (p *commandProcess) Close() {
if p.anchorInput != nil {
_ = p.anchorInput.Close()
p.anchorInput = nil
}
if p.anchor != nil {
_ = p.anchor.Wait()
p.anchor = nil
}
p.groupID = 0
}
66 changes: 66 additions & 0 deletions internal/config/process_posix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build !windows

package config

import (
"os/exec"
"syscall"
"testing"
)

func TestCommandProcessPreservesSysProcAttr(t *testing.T) {
cmd := exec.Command("sh", "-c", "exit 0")
attr := &syscall.SysProcAttr{}
cmd.SysProcAttr = attr

proc, err := startCommandProcess(cmd)
if err != nil {
t.Fatalf("startCommandProcess: %v", err)
}
defer proc.Close()

if cmd.SysProcAttr != attr {
t.Fatal("startCommandProcess replaced SysProcAttr")
}
if !cmd.SysProcAttr.Setpgid || cmd.SysProcAttr.Pgid != proc.groupID {
t.Fatalf("process group = Setpgid %v, Pgid %d; want true, %d", cmd.SysProcAttr.Setpgid, cmd.SysProcAttr.Pgid, proc.groupID)
}
if err := cmd.Wait(); err != nil {
t.Fatalf("Wait: %v", err)
}
}

func TestCommandProcessRetainsGroupIdentityAfterProviderWait(t *testing.T) {
cmd := exec.Command("sh", "-c", "exit 7")
proc, err := startCommandProcess(cmd)
if err != nil {
t.Fatalf("startCommandProcess: %v", err)
}
defer func() {
proc.Terminate()
proc.Close()
}()

providerPID := cmd.Process.Pid
anchorPID := proc.groupID
if err := cmd.Wait(); err == nil {
t.Fatal("Wait error = nil, want nonzero exit")
}
if anchorPID == providerPID {
t.Fatalf("group identity reused provider PID %d", providerPID)
}
if !processAlive(anchorPID) {
t.Fatalf("group leader %d exited before cleanup", anchorPID)
}

proc.Terminate()
proc.Close()
if processAlive(anchorPID) {
t.Fatalf("group leader %d still alive after cleanup", anchorPID)
}
}

func processAlive(pid int) bool {
err := syscall.Kill(pid, 0)
return err == nil || err == syscall.EPERM
}
Loading
Loading