Skip to content

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

Go

fix(codex): retry thread setup handshake timeouts after confirmed cleanup (#11185)

Yan Lobaucfc244740841
diff --git a/server/pkg/agent/codex.go b/server/pkg/agent/codex.go
index a0de3ef842..38c23614a7 100644
--- a/server/pkg/agent/codex.go
+++ b/server/pkg/agent/codex.go
@@ -1570,6 +1570,14 @@ func (b *codexBackend) executeOnce(ctx context.Context, prompt string, opts Exec
finalStatus = "failed"
stderrTail := sanitizeCodexDiagnostic(stderrBuf.Tail())
finalError = err.Error()
+ // No turn has started, so a reaped process tree makes the timeout safe
+ // for the daemon's bounded retry path.
+ retrySafe := timedOut && cleanupConfirmed
+ if retrySafe {
+ finalStatus = "timeout"
+ } else if timedOut {
+ finalError += "; retry suppressed: process cleanup/reap not confirmed"
+ }
if c.threadSetupMethod != "" {
classification := classifyCodexStartupStderr(stderrTail, timedOut)
b.cfg.Logger.Warn("codex lifecycle",
@@ -1584,7 +1592,7 @@ func (b *codexBackend) executeOnce(ctx context.Context, prompt string, opts Exec
"latency_ms", time.Since(c.threadSetupStarted).Milliseconds(),
"cleanup_confirmed", cleanupConfirmed,
"reaped", cleanupConfirmed,
- "retry_safe", false,
+ "retry_safe", retrySafe,
"retry_attempted", false,
"stderr_model_refresh_failure_count", classification.modelRefreshFailure,
"stderr_model_refresh_timeout_count", classification.modelRefreshTimeout,
diff --git a/server/pkg/agent/codex_interrupted_run_test.go b/server/pkg/agent/codex_interrupted_run_test.go
new file mode 100644
index 0000000000..74e7620fc3
--- /dev/null
+++ b/server/pkg/agent/codex_interrupted_run_test.go
@@ -0,0 +1,83 @@
+package agent
+
+import (
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestCodexThreadSetupHandshakeTimeout(t *testing.T) {
+ // Sequential: the negative seam changes the package cleanup override.
+ t.Cleanup(func() { codexCleanupConfirmationOverride.Store(0) })
+ if runtime.GOOS == "windows" {
+ t.Skip("shell-script fixture is POSIX-only")
+ }
+ for _, confirmed := range []bool{true, false} {
+ codexCleanupConfirmationOverride.Store(0)
+ if !confirmed {
+ codexCleanupConfirmationOverride.Store(-1)
+ }
+ for _, resume := range []string{"", "retained-thread"} {
+ method := "thread/start"
+ if resume != "" {
+ method = "thread/resume"
+ }
+ name := method + "/confirmed"
+ if !confirmed {
+ name = method + "/unconfirmed"
+ }
+ t.Run(name, func(t *testing.T) {
+ fake := writeFakeCodexAppServer(t, `read line
+echo '{"jsonrpc":"2.0","id":1,"result":{}}'
+read line
+read line
+# Keep the transport open without answering thread/start or thread/resume.
+while read line; do :; done
+`)
+ work := t.TempDir()
+ marker := filepath.Join(work, "existing-work.txt")
+ if err := os.WriteFile(marker, []byte("retained work"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ result := executeFakeCodex(t, fake, ExecOptions{
+ Cwd: work, ResumeSessionID: resume, Timeout: 15 * time.Second,
+ HandshakeTimeout: 5 * time.Second, ThreadHandshakeTimeout: 200 * time.Millisecond,
+ })
+ if !strings.Contains(result.Error, method+" did not respond after") {
+ t.Fatalf("wrong failure boundary: %+v", result)
+ }
+ if confirmed {
+ if result.Status != "timeout" || strings.Contains(result.Error, "retry suppressed") {
+ t.Fatalf("confirmed cleanup should report a retryable timeout: %+v", result)
+ }
+ } else if result.Status != "failed" || !strings.HasSuffix(result.Error, "; retry suppressed: process cleanup/reap not confirmed") {
+ t.Fatalf("unconfirmed cleanup should stay a plain failure: %+v", result)
+ }
+ if data, err := os.ReadFile(marker); err != nil || string(data) != "retained work" {
+ t.Fatalf("existing work changed: %q %v", data, err)
+ }
+ })
+ }
+ }
+}
+
+func TestCodexThreadSetupErrorWithUnconfirmedCleanupStaysFailed(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("shell-script fixture is POSIX-only")
+ }
+ t.Cleanup(func() { codexCleanupConfirmationOverride.Store(0) })
+ codexCleanupConfirmationOverride.Store(-1)
+ fake := writeFakeCodexAppServer(t, `read line
+echo '{"jsonrpc":"2.0","id":1,"result":{}}'
+read line
+read line
+echo '{"jsonrpc":"2.0","id":2,"error":{"code":-32600,"message":"model not available"}}'
+`)
+ result := executeFakeCodex(t, fake, ExecOptions{Cwd: t.TempDir(), Timeout: 15 * time.Second, HandshakeTimeout: 5 * time.Second})
+ if result.Status != "failed" || !strings.Contains(result.Error, "model not available") || strings.Contains(result.Error, "retry suppressed") {
+ t.Fatalf("non-timeout thread/start error should be a plain failure: %+v", result)
+ }
+}
diff --git a/server/pkg/agent/proc_windows_test.go b/server/pkg/agent/proc_windows_test.go
index c266198583..95a39f45b6 100644
--- a/server/pkg/agent/proc_windows_test.go
+++ b/server/pkg/agent/proc_windows_test.go
@@ -410,8 +410,8 @@ func main() {
if elapsed := time.Since(started); elapsed > 3*time.Second {
t.Fatalf("cleanup exceeded bound: %s", elapsed)
}
- if result.Status != "failed" || !strings.Contains(result.Error, "thread/start") {
- t.Fatalf("expected thread/start failure, got %+v", result)
+ if result.Status != "timeout" || !strings.Contains(result.Error, "thread/start") {
+ t.Fatalf("expected confirmed-cleanup thread/start timeout, got %+v", result)
}
entries := parseJSONLogEntries(t, logs.String())
failure := findCodexLifecyclePhase(t, entries, "thread_start_failure")