Skip to content

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

Go

fix(codex): MUL-7381 synchronize codex thread ID access (#8428)

Bohan Jianga00ebbb952f5
diff --git a/server/pkg/agent/codex.go b/server/pkg/agent/codex.go
index 18b82bbf75..6fb84d8b7e 100644
--- a/server/pkg/agent/codex.go
+++ b/server/pkg/agent/codex.go
@@ -1541,7 +1541,7 @@ func (b *codexBackend) executeOnce(ctx context.Context, prompt string, opts Exec
}
return
}
- c.threadID = threadID
+ c.setThreadID(threadID)
if resumed {
b.cfg.Logger.Info("codex thread resumed", "thread_id", threadID)
} else {
@@ -2303,6 +2303,7 @@ type codexClient struct {
activeLaunches int64
threadSetupMethod string
threadSetupStarted time.Time
+ threadIDMu sync.RWMutex
threadID string
turnIDMu sync.RWMutex
turnID string
@@ -2422,6 +2423,22 @@ func (c *codexClient) getTurnError() string {
return c.turnError
}
+// setThreadID publishes the thread ID resolved by thread/start or
+// thread/resume. The task goroutine writes it while the stdout goroutine is
+// already dispatching notifications for that same thread, so the field needs
+// the same synchronization as turnID (GH #8422).
+func (c *codexClient) setThreadID(threadID string) {
+ c.threadIDMu.Lock()
+ c.threadID = threadID
+ c.threadIDMu.Unlock()
+}
+
+func (c *codexClient) getThreadID() string {
+ c.threadIDMu.RLock()
+ defer c.threadIDMu.RUnlock()
+ return c.threadID
+}
+
func (c *codexClient) setActiveTurnID(turnID string) {
if turnID == "" {
return
@@ -3256,7 +3273,7 @@ func (c *codexClient) handleEvent(msg map[string]any) {
switch msgType {
case "task_started":
if c.onMessage != nil {
- c.onMessage(Message{Type: MessageStatus, Status: "running", SessionID: c.threadID})
+ c.onMessage(Message{Type: MessageStatus, Status: "running", SessionID: c.getThreadID()})
}
case "agent_message":
text, _ := msg["message"].(string)
@@ -3356,7 +3373,7 @@ func (c *codexClient) handleRawNotification(method string, params map[string]any
c.setActiveTurnID(turnID)
}
if c.onMessage != nil {
- c.onMessage(Message{Type: MessageStatus, Status: "running", SessionID: c.threadID})
+ c.onMessage(Message{Type: MessageStatus, Status: "running", SessionID: c.getThreadID()})
}
case "turn/completed":
@@ -3426,7 +3443,13 @@ func (c *codexClient) handleRawNotification(method string, params map[string]any
func (c *codexClient) isNotificationFromOtherThread(params map[string]any) bool {
threadID, ok := params["threadId"].(string)
- return ok && c.threadID != "" && threadID != c.threadID
+ if !ok {
+ return false
+ }
+ // Compare against one snapshot: this runs on the stdout goroutine, ahead of
+ // the current-turn gate, so the gate's state cannot serialize the read.
+ currentThreadID := c.getThreadID()
+ return currentThreadID != "" && threadID != currentThreadID
}
func (c *codexClient) handleItemNotification(method string, params map[string]any) {
diff --git a/server/pkg/agent/codex_thread_id_race_test.go b/server/pkg/agent/codex_thread_id_race_test.go
new file mode 100644
index 0000000000..c361aa4c3d
--- /dev/null
+++ b/server/pkg/agent/codex_thread_id_race_test.go
@@ -0,0 +1,74 @@
+package agent
+
+import (
+ "runtime"
+ "testing"
+ "time"
+)
+
+// GH #8422: the app-server may emit a valid notification immediately after the
+// thread/start response, while executeOnce is still publishing the returned
+// thread ID. The stdout goroutine then reads c.threadID in the thread filter
+// concurrently with that write. The filter runs ahead of the current-turn gate,
+// so the gate's own state cannot serialize this access.
+func TestCodexThreadIDPublishedBeforeNotificationFilter(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("shell-script fixture is POSIX-only")
+ }
+ fakePath := writeFakeCodexAppServer(t, `read line
+echo '{"jsonrpc":"2.0","id":1,"result":{}}'
+read line
+read line
+echo '{"jsonrpc":"2.0","id":2,"result":{"thread":{"id":"thr-race"}}}'
+echo '{"jsonrpc":"2.0","method":"thread/status/changed","params":{"threadId":"thr-race","status":{"type":"idle"}}}'
+read line
+echo '{"jsonrpc":"2.0","method":"turn/started","params":{"threadId":"thr-race","turn":{"id":"turn-race"}}}'
+echo '{"jsonrpc":"2.0","method":"item/completed","params":{"threadId":"thr-race","item":{"type":"agentMessage","id":"msg-race","text":"Done"}}}'
+echo '{"jsonrpc":"2.0","method":"turn/completed","params":{"threadId":"thr-race","turn":{"id":"turn-race","status":"completed"}}}'
+`)
+ result := executeFakeCodex(t, fakePath, ExecOptions{Timeout: 5 * time.Second})
+ if result.Status != "completed" || result.Output != "Done" {
+ t.Fatalf("unexpected result: status=%q output=%q error=%q", result.Status, result.Output, result.Error)
+ }
+}
+
+// The thread filter is the first thing every notification passes through, so it
+// must tolerate absent, null and non-string threadId rather than take the
+// process down with it (GH #8422).
+func TestCodexThreadFilterToleratesMalformedParams(t *testing.T) {
+ c, _, _ := newTestCodexClient(t)
+ c.setThreadID("thr-main")
+ for _, params := range []map[string]any{
+ nil, {}, {"threadId": nil}, {"threadId": 42},
+ {"threadId": []any{}}, {"threadId": map[string]any{}},
+ } {
+ if c.isNotificationFromOtherThread(params) {
+ t.Fatalf("unexpected other-thread match for %#v", params)
+ }
+ }
+ for _, line := range []string{
+ `{"method":"thread/status/changed"}`,
+ `{"method":"thread/status/changed","params":null}`,
+ `{"method":"thread/status/changed","params":[]}`,
+ `{"method":"thread/status/changed","params":42}`,
+ `{"method":"thread/status/changed","params":{"threadId":null}}`,
+ } {
+ c.handleLine(line)
+ }
+}
+
+// A notification genuinely belonging to another thread must still be filtered
+// out once the current thread ID is known.
+func TestCodexThreadFilterStillRejectsOtherThread(t *testing.T) {
+ c, _, _ := newTestCodexClient(t)
+ if c.isNotificationFromOtherThread(map[string]any{"threadId": "thr-other"}) {
+ t.Fatal("no current thread ID yet: nothing to filter against")
+ }
+ c.setThreadID("thr-main")
+ if !c.isNotificationFromOtherThread(map[string]any{"threadId": "thr-other"}) {
+ t.Fatal("expected other-thread notification to be filtered")
+ }
+ if c.isNotificationFromOtherThread(map[string]any{"threadId": "thr-main"}) {
+ t.Fatal("expected own-thread notification to pass")
+ }
+}