Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(daemon): close stdin pipe in Pi adapter to deliver EOF (#2188) (#3118)

Wescfc652aa5fc3
diff --git a/server/pkg/agent/pi.go b/server/pkg/agent/pi.go
index 03b5390814..66fd37110f 100644
--- a/server/pkg/agent/pi.go
+++ b/server/pkg/agent/pi.go
@@ -220,12 +220,26 @@ func (b *piBackend) Execute(ctx context.Context, prompt string, opts ExecOptions
cancel()
return nil, fmt.Errorf("pi stdout pipe: %w", err)
}
+ // Attach an explicit stdin pipe so we can close it ourselves. Pi reads
+ // its prompt from argv (positional, see buildPiArgs) and never expects
+ // interactive input, but when the parent leaves cmd.Stdin nil and the
+ // daemon is run under systemd, Pi has been observed to block in its
+ // event loop awaiting stdin events instead of progressing to "done"
+ // (#2188). Closing the pipe immediately after Start delivers an
+ // explicit EOF on a FIFO, which unblocks Pi's readable side.
+ stdin, err := cmd.StdinPipe()
+ if err != nil {
+ cancel()
+ return nil, fmt.Errorf("pi stdin pipe: %w", err)
+ }
cmd.Stderr = newLogWriter(b.cfg.Logger, "[pi:stderr] ")
if err := cmd.Start(); err != nil {
+ _ = stdin.Close()
cancel()
return nil, fmt.Errorf("start pi: %w", err)
}
+ _ = stdin.Close()
b.cfg.Logger.Info("pi started", "pid", cmd.Process.Pid, "cwd", opts.Cwd, "model", opts.Model)
diff --git a/server/pkg/agent/pi_test.go b/server/pkg/agent/pi_test.go
index 694fec9a17..a8423d38e9 100644
--- a/server/pkg/agent/pi_test.go
+++ b/server/pkg/agent/pi_test.go
@@ -1,9 +1,13 @@
package agent
import (
+ "context"
"log/slog"
+ "path/filepath"
+ "runtime"
"strings"
"testing"
+ "time"
)
func TestBuildPiArgsNoToolAllowlist(t *testing.T) {
@@ -54,6 +58,67 @@ func TestBuildPiArgsCustomArgsAppended(t *testing.T) {
}
}
+// TestPiExecuteAttachesStdinPipe verifies that the Pi backend spawns the
+// child with an explicit stdin pipe (FIFO) instead of leaving cmd.Stdin
+// nil. Without an explicit pipe, Pi has been observed to block under
+// systemd waiting for stdin events (#2188); attaching and immediately
+// closing a pipe delivers a clean EOF on a FIFO and unblocks Pi.
+//
+// The probe is structural rather than behavioral: a shell script in
+// place of `pi` inspects /proc/self/fd/0 and only emits a valid event
+// stream if stdin is a FIFO. If the fix regresses (stdin nil → /dev/null
+// char device), the fake exits non-zero and the test fails.
+func TestPiExecuteAttachesStdinPipe(t *testing.T) {
+ t.Parallel()
+ if runtime.GOOS != "linux" {
+ // /proc/self/fd/0 is Linux-specific; skipping elsewhere keeps
+ // the assertion portable without losing CI coverage.
+ t.Skip("stdin fd inspection relies on /proc/self/fd/0")
+ }
+
+ fakePath := filepath.Join(t.TempDir(), "pi")
+ script := "#!/bin/sh\n" +
+ "kind=$(stat -c '%F' -L /proc/self/fd/0 2>/dev/null || echo unknown)\n" +
+ "case \"$kind\" in\n" +
+ " fifo|*pipe*)\n" +
+ " printf '%s\\n' '{\"type\":\"agent_start\"}'\n" +
+ " printf '%s\\n' '{\"type\":\"turn_end\",\"message\":{\"role\":\"assistant\",\"model\":\"test\",\"usage\":{\"input\":1,\"output\":1,\"cacheRead\":0,\"cacheWrite\":0,\"totalTokens\":2}}}'\n" +
+ " exit 0\n" +
+ " ;;\n" +
+ "esac\n" +
+ "printf 'stdin was %s; expected fifo\\n' \"$kind\" >&2\n" +
+ "exit 1\n"
+ writeTestExecutable(t, fakePath, []byte(script))
+
+ backend, err := New("pi", Config{ExecutablePath: fakePath, Logger: slog.Default()})
+ if err != nil {
+ t.Fatalf("new pi backend: %v", err)
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
+ defer cancel()
+ session, err := backend.Execute(ctx, "prompt-ignored", ExecOptions{Timeout: 5 * time.Second})
+ if err != nil {
+ t.Fatalf("execute: %v", err)
+ }
+ go func() {
+ for range session.Messages {
+ }
+ }()
+
+ select {
+ case result, ok := <-session.Result:
+ if !ok {
+ t.Fatal("result channel closed without a value")
+ }
+ if result.Status != "completed" {
+ t.Fatalf("expected status=completed (stdin attached as fifo), got %q (error=%q)", result.Status, result.Error)
+ }
+ case <-time.After(10 * time.Second):
+ t.Fatal("timeout waiting for result")
+ }
+}
+
func TestStripPiToolCallMarkup(t *testing.T) {
tests := map[string]string{
`before call:bash{command:<|"|>cd repo/path && ls -F<|"|>}<tool_call|> after`: "before after",