From 1e71d048e3be12b007f2cdd7f0e6b339cb218516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Flor=C3=AAncio?= Date: Wed, 15 Apr 2026 23:22:15 +0100 Subject: [PATCH] fix: skip ExtraFiles liveness pipe on Windows to fix agentctl startup (#599) --- .../agentctl/client/launcher/launcher.go | 20 +++--- .../client/launcher/launcher_pipe_unix.go | 44 +++++++++++++ .../launcher/launcher_pipe_unix_test.go | 63 +++++++++++++++++++ .../client/launcher/launcher_pipe_windows.go | 22 +++++++ .../launcher/launcher_pipe_windows_test.go | 24 +++++++ .../agentctl/client/launcher/launcher_test.go | 2 - 6 files changed, 162 insertions(+), 13 deletions(-) create mode 100644 apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix.go create mode 100644 apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix_test.go create mode 100644 apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows.go create mode 100644 apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows_test.go diff --git a/apps/backend/internal/agentctl/client/launcher/launcher.go b/apps/backend/internal/agentctl/client/launcher/launcher.go index f9a6eef5f1..66eb90c794 100644 --- a/apps/backend/internal/agentctl/client/launcher/launcher.go +++ b/apps/backend/internal/agentctl/client/launcher/launcher.go @@ -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)) diff --git a/apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix.go b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix.go new file mode 100644 index 0000000000..a536a2942f --- /dev/null +++ b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix.go @@ -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() + } +} diff --git a/apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix_test.go b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix_test.go new file mode 100644 index 0000000000..7d413bb2c8 --- /dev/null +++ b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_unix_test.go @@ -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") + } +} diff --git a/apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows.go b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows.go new file mode 100644 index 0000000000..5da371c503 --- /dev/null +++ b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows.go @@ -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) {} diff --git a/apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows_test.go b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows_test.go new file mode 100644 index 0000000000..04b6ac4150 --- /dev/null +++ b/apps/backend/internal/agentctl/client/launcher/launcher_pipe_windows_test.go @@ -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)) + } +} diff --git a/apps/backend/internal/agentctl/client/launcher/launcher_test.go b/apps/backend/internal/agentctl/client/launcher/launcher_test.go index 94a9b0b7ec..1f1979712b 100644 --- a/apps/backend/internal/agentctl/client/launcher/launcher_test.go +++ b/apps/backend/internal/agentctl/client/launcher/launcher_test.go @@ -1,5 +1,3 @@ -//go:build !windows - package launcher import (