Repository navigation
[fix]: core - scope the stream terminal marker to the active attempt - #10290
ahmedabdou14 wants to merge 3 commits into
Conversation
A stream attempt that failed on its first chunk left the terminal marker set on the shared context. If a retry or fallback attempt then panicked, RecoverStreamPanic treated the stream as already ended, so neither the client nor the post-hooks saw the served attempt end. The marker is now cleared wherever the other per-attempt stream flags are. Affected packages: - core/providers/utils/utils.go - core/bifrost.go - core/utils.go - core/streamfallback_test.go, core/bifrost_test.go - core/changelog.md Fixes maximhq#10202
|
Automatic approval withheld CodeRabbit could not verify the path filters used for the saved review. Run a full review using the repository's approval configuration. A reviewer can post Commit: No actionable comments were generated in the recent review. Summary
Merge Risk:
|
akshaydeo
left a comment
There was a problem hiding this comment.
Thanks for this, it is a clean, well-scoped fix. I confirmed red before green: with either reset removed, the matching TestStreamPanicAfterRetryOrFallbackReachesPostHooks case fails (no client error chunk, no panic post-hook), and both pass at this head, also under -race. The reset is safe because both call sites run only after the previous attempt's channel has been drained (<-drainDone in executeRequestWithRetries), and nothing marks the terminal after close(responseChan). The cost statement and the harness exemption in the PR body read fine to me. Two small nits inline, neither blocks merge.
| ctx.ClearValue(schemas.BifrostContextKeyStreamEndIndicator) | ||
| ctx.ClearValue(schemas.BifrostContextKeyStreamBodyExhausted) | ||
| ctx.ClearValue(schemas.BifrostContextKeyStreamParkedAfterFinish) | ||
| providerUtils.ResetStreamTerminalHandled(ctx) |
There was a problem hiding this comment.
Nit (follow-up is fine): this block and clearCtxForFallback (core/utils.go:463-468) now keep two hand-copied lists of the same per-attempt stream state (StreamEndIndicator, StreamBodyExhausted, StreamParkedAfterFinish, and now the terminal marker). This bug happened because a marker was added to neither list. A small shared helper, for example resetStreamAttemptState(ctx), called from both places would keep the next stream flag from being missed in one of them.
| @@ -1,3 +1,4 @@ | |||
| - fix: a retry or fallback stream that panics now ends with an error chunk and an error post-hook, even after an earlier attempt failed on its first chunk (#10202) [@ahmedabdou14](https://github.com/ahmedabdou14) | |||
There was a problem hiding this comment.
Nit: other credited entries in this file use (thanks [@user](https://github.com/user)!). Could you match that, e.g. ... on its first chunk (#10202) (thanks [@ahmedabdou14](https://github.com/ahmedabdou14)!)?
…and fallback The retry block in executeRequestWithRetries and clearCtxForFallback each kept their own list of per-attempt stream flags to clear. Both now call ResetStreamAttemptState, which replaces ResetStreamTerminalHandled, so a new flag cannot be added to one list and missed in the other. No behavior change. The changelog credit now matches the file's style. Affected packages: - core/providers/utils/utils.go - core/bifrost.go - core/utils.go - core/changelog.md
Confidence Score: 4/5
[Medium impact] The change affects streaming requests that enter a retry or fallback. The code change appears sound, but the test comment must follow the repository’s explicit comment rule before merging. Findings
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Attempt fails before useful output] --> B[Drain the old stream]
B --> C[Choose retry or fallback]
C --> D[Reset stream attempt state]
D --> E[Start the next attempt]
E --> F{Provider goroutine panics?}
F -->|Yes| G[Send error through post-hooks]
G --> H[Client receives terminal error]
F -->|No| I[Continue the stream]
Reviews (2) · Last reviewed commit: "merge dev into fix/10202-attempt-scoped-..." · Reviewed by Greptile |
| // earlier attempt's terminal bookkeeping belongs to that attempt; before the fix it | ||
| // survived the retry or fallback, so the recovered panic sent nothing and plugins never | ||
| // saw the served attempt end. |
There was a problem hiding this comment.
Test comment describes old behavior
The new comment explains what happened “before the fix.” AGENTS.md requires test comments to describe the behavior they check, not the incident or previous implementation. Remove the historical explanation and keep the expected behavior. This repository requirement must be satisfied before merging.
| // earlier attempt's terminal bookkeeping belongs to that attempt; before the fix it | |
| // survived the retry or fallback, so the recovered panic sent nothing and plugins never | |
| // saw the served attempt end. | |
| // earlier attempt's terminal bookkeeping belongs to that attempt. |
File Used: AGENTS.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Summary
Fixes #10202. Follow-up to the stream panic recovery from #9791.
A stream attempt that fails on its first chunk (an error event in a 200 stream, or a TTFT cut) sends its error through
ProcessAndSendBifrostError, which marks the shared context as having handled its terminal chunk. Retries and fallbacks reuse that context and clear the other per-stream flags, but not this marker. If the next attempt's provider goroutine then panics,RecoverStreamPanicsees the old marker and sends nothing. The client stream ends with no error and no post-hook runs for the attempt that served the request, so logging, governance and usage tracking never see it end.Changes
ResetStreamAttemptStateincore/providers/utilsclears the per-attempt stream state:StreamEndIndicator,StreamBodyExhausted,StreamParkedAfterFinishand the terminal marker.executeRequestWithRetriesandclearCtxForFallbackboth call it instead of keeping their own lists, so a new flag cannot be missed in one of them. Both run after the previous attempt's stream has drained, so no late write can race the reset.core/changelog.md.Not taken: keying the marker to the attempt's response channel. It needs every provider to pass the same channel to the send helpers and to
RecoverStreamPanic, and a mismatch would send a second terminal after a good final chunk.Type of change
Affected areas
How to test
TestStreamPanicAfterRetryOrFallbackReachesPostHookshas aretryand afallbackcase: the first attempt fails with an SSE rate-limit event, and a test tracer panics on the served attempt's second chunk. It expects one client error chunk and one error post-hook withErrStreamPanicMessage. Both cases fail ondevand pass here; each fails when only its own call site is reverted.The CI core unit set (
go testover every core package except the llmtests ones) passes, apart frominternal/memtest(TestEveryLoopBoundJSONMutationIsReviewed) and theinternal/mcptestsSTDIO cases, which fail the same way on unmodifieddevlocally (Go 1.27.4, darwin/arm64).No provider-harness case: the change only shows on the wire when a provider goroutine panics, which no well-formed HTTP request can trigger.
Cost (
BenchmarkClearCtxForFallback,-benchmem, Go 1.27.4, Apple M4 Max, median of 6): 290 ns/op before, 315 ns/op after, 0 allocs/op and 0 B/op both. The extra cost is one context value write, paid only on a retry or fallback attempt, never on a request served by its first attempt. No memory is retained.Screenshots/Recordings
N/A
Breaking changes
Related issues
Fixes #10202
Security considerations
None.
Checklist