Skip to content

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

Go

fix(daemon): bound runtime --version probe so one wedged CLI can't block all runtimes (MUL-3812) (#4685)

Bohan Jiang4fb6c0fb0e09
diff --git a/server/pkg/agent/agent_test.go b/server/pkg/agent/agent_test.go
index de04ad7e2c..69ffaacdce 100644
--- a/server/pkg/agent/agent_test.go
+++ b/server/pkg/agent/agent_test.go
@@ -2,6 +2,11 @@ package agent
import (
"context"
+ "fmt"
+ "os"
+ "path/filepath"
+ "runtime"
+ "strconv"
"strings"
"testing"
"time"
@@ -98,6 +103,69 @@ func TestDetectVersionFailsForMissingBinary(t *testing.T) {
}
}
+// TestDetectVersionTimesOutOnHang guards MUL-3812: a CLI whose `--version`
+// never returns (e.g. a brew-installed claude wedged by a bun regression) must
+// not stall version detection forever. The daemon detects every runtime's
+// version sequentially inside its blocking preflight, so an unbounded probe
+// would leave the daemon stuck "starting" and *every* runtime on the host
+// disconnected. detectCLIVersion must bound the probe and return an error so
+// the registration loop isolates the broken runtime and the rest still
+// register. The script also leaves an orphaned child holding the stdout pipe
+// open after the parent is killed, exercising the cmd.WaitDelay path.
+func TestDetectVersionTimesOutOnHang(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("relies on a /bin/sh hang script")
+ }
+
+ dir := t.TempDir()
+ script := filepath.Join(dir, "hang.sh")
+ pidFile := filepath.Join(dir, "child.pid")
+ // The CLI hangs forever (`wait`) and backgrounds a child that inherits and
+ // holds our stdout pipe open even after the parent is killed on timeout —
+ // the exact case cmd.WaitDelay must cover. The child records its PID so we
+ // can reap it in Cleanup instead of leaking a 60s `sleep` into CI.
+ body := fmt.Sprintf("#!/bin/sh\nsleep 60 &\necho $! > %q\nwait\n", pidFile)
+ if err := os.WriteFile(script, []byte(body), 0o755); err != nil {
+ t.Fatalf("write hang script: %v", err)
+ }
+ t.Cleanup(func() {
+ data, err := os.ReadFile(pidFile)
+ if err != nil {
+ return // child never recorded its PID; nothing to reap
+ }
+ pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
+ if err != nil {
+ return
+ }
+ if proc, err := os.FindProcess(pid); err == nil {
+ _ = proc.Kill()
+ }
+ })
+
+ orig := detectVersionTimeout
+ detectVersionTimeout = 200 * time.Millisecond
+ t.Cleanup(func() { detectVersionTimeout = orig })
+
+ done := make(chan error, 1)
+ start := time.Now()
+ go func() {
+ _, err := DetectVersion(context.Background(), script)
+ done <- err
+ }()
+
+ select {
+ case err := <-done:
+ if err == nil {
+ t.Fatal("expected an error from a hanging --version probe, got nil")
+ }
+ if elapsed := time.Since(start); elapsed > 5*time.Second {
+ t.Fatalf("detection took %v; expected it to be bounded by the timeout", elapsed)
+ }
+ case <-time.After(10 * time.Second):
+ t.Fatal("DetectVersion did not return: version probe is unbounded (regression of MUL-3812)")
+ }
+}
+
func TestLaunchHeaderCoversAllSupportedBackends(t *testing.T) {
t.Parallel()
diff --git a/server/pkg/agent/claude.go b/server/pkg/agent/claude.go
index 41d7ac9cf9..88a0559640 100644
--- a/server/pkg/agent/claude.go
+++ b/server/pkg/agent/claude.go
@@ -804,9 +804,30 @@ func writeMcpConfigToTemp(raw json.RawMessage) (string, error) {
return f.Name(), nil
}
+// detectVersionTimeout bounds a single `<cli> --version` probe. Version
+// detection runs inside the daemon's blocking preflight (registerRuntimesForWorkspace),
+// so a CLI that never returns from `--version` — e.g. a brew-installed claude
+// wedged by a bun regression (MUL-3812) — would otherwise stall the whole
+// registration loop, the daemon would never flip /health from "starting" to
+// "running", and *every* runtime on the host would appear disconnected. A real
+// `--version` returns well under this bound even on a cold cache or with
+// Windows AV scanning; the timeout exists only to fail a wedged probe fast and
+// in isolation so the remaining runtimes still register. A var (not const) so
+// tests can shrink it without waiting out the real bound.
+var detectVersionTimeout = 10 * time.Second
+
func detectCLIVersion(ctx context.Context, execPath string) (string, error) {
+ ctx, cancel := context.WithTimeout(ctx, detectVersionTimeout)
+ defer cancel()
+
cmd := exec.CommandContext(ctx, execPath, "--version")
hideAgentWindow(cmd)
+ // exec.CommandContext only kills the direct child on timeout. A broken CLI
+ // (node/bun shim) can leave grandchildren that inherited and still hold our
+ // stdout pipe open, and cmd.Output() blocks in Wait() until that pipe
+ // closes — defeating the timeout above. WaitDelay forces the pipes shut and
+ // reaps shortly after the context fires so this call always returns.
+ cmd.WaitDelay = 2 * time.Second
data, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("detect version for %s: %w", execPath, err)