fix: skip ExtraFiles liveness pipe on Windows to fix agentctl startup (#599)
This commit is contained in:
@@ -151,16 +151,14 @@ func (l *Launcher) Start(ctx context.Context) error {
|
||||
// - Pdeathsig on Linux: kernel sends SIGTERM to child when parent dies.
|
||||
l.cmd.SysProcAttr = buildSysProcAttr()
|
||||
|
||||
// Parent liveness pipe: agentctl inherits the read-end (FD 3 via ExtraFiles)
|
||||
// and blocks on it. When this process dies — even via SIGKILL — the kernel
|
||||
// closes the write-end, breaking the pipe. agentctl detects the break and
|
||||
// initiates graceful shutdown. This is the portable equivalent of Pdeathsig.
|
||||
pipeRead, pipeWrite, err := os.Pipe()
|
||||
// Parent liveness pipe (Unix only): agentctl inherits the read-end via
|
||||
// ExtraFiles and blocks on it. When the parent dies the kernel closes the
|
||||
// write-end, signaling agentctl to shut down. On Windows this is a no-op
|
||||
// because ExtraFiles is not supported; cleanup relies on gracefulStop.
|
||||
pipeWrite, err := setupLivenessPipe(l.cmd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create parent liveness pipe: %w", err)
|
||||
return err
|
||||
}
|
||||
l.cmd.ExtraFiles = []*os.File{pipeRead} // child FD 3
|
||||
l.cmd.Env = append(l.cmd.Env, "KANDEV_PARENT_PIPE_FD=3")
|
||||
|
||||
// Capture stdout and stderr
|
||||
stdout, err := l.cmd.StdoutPipe()
|
||||
@@ -174,14 +172,14 @@ func (l *Launcher) Start(ctx context.Context) error {
|
||||
|
||||
// Start the process
|
||||
if err := l.cmd.Start(); err != nil {
|
||||
_ = pipeRead.Close()
|
||||
_ = pipeWrite.Close()
|
||||
closePipeOnStartFailure(pipeWrite, l.cmd)
|
||||
return fmt.Errorf("failed to start agentctl: %w", err)
|
||||
}
|
||||
|
||||
// Close read-end in parent (child inherited it). Keep write-end open —
|
||||
// it closes automatically when this process exits, signaling agentctl.
|
||||
_ = pipeRead.Close()
|
||||
// On Windows these are no-ops (no liveness pipe); parentPipe stays nil.
|
||||
closeChildPipeEnd(l.cmd)
|
||||
l.parentPipe = pipeWrite
|
||||
|
||||
l.logger.Info("agentctl process started", zap.Int("pid", l.cmd.Process.Pid))
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
//go:build !windows
|
||||
|
||||
package launcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// setupLivenessPipe creates a pipe and configures the command to pass the
|
||||
// read-end to the child via ExtraFiles (FD 3). The child monitors this pipe;
|
||||
// when the parent dies the kernel closes the write-end, breaking the pipe and
|
||||
// signaling the child to shut down. Returns the write-end (caller must keep it
|
||||
// open) or an error.
|
||||
func setupLivenessPipe(cmd *exec.Cmd) (*os.File, error) {
|
||||
pipeRead, pipeWrite, err := os.Pipe()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create parent liveness pipe: %w", err)
|
||||
}
|
||||
|
||||
cmd.ExtraFiles = []*os.File{pipeRead} // child FD 3
|
||||
cmd.Env = append(cmd.Env, "KANDEV_PARENT_PIPE_FD=3")
|
||||
|
||||
return pipeWrite, nil
|
||||
}
|
||||
|
||||
// closePipeOnStartFailure cleans up both pipe ends when cmd.Start fails.
|
||||
func closePipeOnStartFailure(pipeWrite *os.File, cmd *exec.Cmd) {
|
||||
if pipeWrite != nil {
|
||||
_ = pipeWrite.Close()
|
||||
}
|
||||
if len(cmd.ExtraFiles) > 0 {
|
||||
_ = cmd.ExtraFiles[0].Close()
|
||||
}
|
||||
}
|
||||
|
||||
// closeChildPipeEnd closes the read-end of the pipe in the parent process
|
||||
// after the child has inherited it.
|
||||
func closeChildPipeEnd(cmd *exec.Cmd) {
|
||||
if len(cmd.ExtraFiles) > 0 {
|
||||
_ = cmd.ExtraFiles[0].Close()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
//go:build !windows
|
||||
|
||||
package launcher
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetupLivenessPipe_SetsExtraFiles(t *testing.T) {
|
||||
cmd := exec.Command("true")
|
||||
cmd.Env = []string{}
|
||||
|
||||
pipeWrite, err := setupLivenessPipe(cmd)
|
||||
if err != nil {
|
||||
t.Fatalf("setupLivenessPipe: %v", err)
|
||||
}
|
||||
defer func() { _ = pipeWrite.Close() }()
|
||||
|
||||
if pipeWrite == nil {
|
||||
t.Fatal("expected non-nil write-end")
|
||||
}
|
||||
if len(cmd.ExtraFiles) != 1 {
|
||||
t.Fatalf("expected 1 ExtraFile, got %d", len(cmd.ExtraFiles))
|
||||
}
|
||||
defer func() { _ = cmd.ExtraFiles[0].Close() }()
|
||||
|
||||
found := false
|
||||
for _, env := range cmd.Env {
|
||||
if strings.HasPrefix(env, "KANDEV_PARENT_PIPE_FD=") {
|
||||
found = true
|
||||
if env != "KANDEV_PARENT_PIPE_FD=3" {
|
||||
t.Errorf("expected KANDEV_PARENT_PIPE_FD=3, got %s", env)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Error("KANDEV_PARENT_PIPE_FD not set in cmd.Env")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupLivenessPipe_PipeIsReadable(t *testing.T) {
|
||||
cmd := exec.Command("true")
|
||||
cmd.Env = []string{}
|
||||
|
||||
pipeWrite, err := setupLivenessPipe(cmd)
|
||||
if err != nil {
|
||||
t.Fatalf("setupLivenessPipe: %v", err)
|
||||
}
|
||||
|
||||
pipeRead := cmd.ExtraFiles[0]
|
||||
|
||||
// Close write-end; read should return immediately (EOF).
|
||||
_ = pipeWrite.Close()
|
||||
|
||||
buf := make([]byte, 1)
|
||||
_, err = pipeRead.Read(buf)
|
||||
_ = pipeRead.Close()
|
||||
if err == nil {
|
||||
t.Error("expected error (EOF) after closing write-end")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
//go:build windows
|
||||
|
||||
package launcher
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
// setupLivenessPipe is a no-op on Windows. ExtraFiles is not supported by
|
||||
// Go's Windows process creation. The agentctl child handles the missing
|
||||
// KANDEV_PARENT_PIPE_FD env var gracefully (monitorParentLiveness returns nil).
|
||||
// On Windows, process cleanup relies on gracefulStop (CTRL_BREAK_EVENT).
|
||||
func setupLivenessPipe(_ *exec.Cmd) (*os.File, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// closePipeOnStartFailure is a no-op on Windows (no pipe to clean up).
|
||||
func closePipeOnStartFailure(_ *os.File, _ *exec.Cmd) {}
|
||||
|
||||
// closeChildPipeEnd is a no-op on Windows (no pipe to close).
|
||||
func closeChildPipeEnd(_ *exec.Cmd) {}
|
||||
@@ -0,0 +1,24 @@
|
||||
//go:build windows
|
||||
|
||||
package launcher
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetupLivenessPipe_NoopOnWindows(t *testing.T) {
|
||||
cmd := exec.Command("cmd", "/c", "echo", "test")
|
||||
cmd.Env = []string{}
|
||||
|
||||
pipeWrite, err := setupLivenessPipe(cmd)
|
||||
if err != nil {
|
||||
t.Fatalf("setupLivenessPipe: %v", err)
|
||||
}
|
||||
if pipeWrite != nil {
|
||||
t.Error("expected nil write-end on Windows")
|
||||
}
|
||||
if len(cmd.ExtraFiles) != 0 {
|
||||
t.Errorf("expected no ExtraFiles on Windows, got %d", len(cmd.ExtraFiles))
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
//go:build !windows
|
||||
|
||||
package launcher
|
||||
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user