Skip to content

fix(aibridge): initiate SSE stream before agentic continuation to avoid IsStreaming race#26139

Open
ssncferreira wants to merge 1 commit into
mainfrom
ssncf/messages-initiate-stream-race
Open

fix(aibridge): initiate SSE stream before agentic continuation to avoid IsStreaming race#26139
ssncferreira wants to merge 1 commit into
mainfrom
ssncf/messages-initiate-stream-race

Conversation

@ssncferreira
Copy link
Copy Markdown
Contributor

@ssncferreira ssncferreira commented Jun 8, 2026

The agentic loop has a race between the main goroutine and the Start goroutine on the shared ResponseWriter. When an iteration's response contains only injected-tool events (no text to relay), Start may not have called InitiateStream by the time main reaches the IsStreaming check on the next iteration. The IsStreaming check then returns false, main writes a JSON error via writeUpstreamError, and Start later writes SSE headers and events on top, producing a malformed JSON+SSE response:

{\"error\":{\"message\":\"all configured keys are rate-limited\",\"type\":\"rate_limit_error\"},\"request_id\":\"\",\"type\":\"error\"}event: message_start\n..."

Fix: explicitly call events.InitiateStream(w) at the agentic continuation point so the SSE stream is committed before the next iteration runs. Keeps messages consistent with the pattern already used in chatcompletions/streaming.go. sync.Once makes the double call safe.

Related: coder/internal#1524
Related: #25654
Closes: https://linear.app/codercom/issue/AIGOV-336/flake-teststreaminginterception-agenticloopfailoveragentic-all-keys

Note

Initially generated by Claude Opus 4.7, modified and reviewed by @ssncferreira

@linear-code
Copy link
Copy Markdown

linear-code Bot commented Jun 8, 2026

AIGOV-336

@ssncferreira ssncferreira force-pushed the ssncf/messages-initiate-stream-race branch from 89e2a12 to 9c691b7 Compare June 8, 2026 14:23
@ssncferreira ssncferreira marked this pull request as ready for review June 8, 2026 15:44
Copy link
Copy Markdown
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix! Are you able to add a test which exposes this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants