Repository navigation
[type:fix] avoid retrying partial AI streams - #7205
Conversation
Aias00
left a comment
There was a problem hiding this comment.
Correct and important fix. Before this change, a stream that emitted some chunks and then failed was retried from scratch and, worse, onErrorResume replayed the whole request through the fallback provider — so the client received duplicated/overlapping content. Gating both retryWhen and onErrorResume on "nothing has been emitted yet" is exactly the right predicate.
Implementation notes I verified:
Flux.defergives each subscription its ownAtomicBoolean, so concurrent subscribers don't share state. Good.retryWhensits inside thedefer, so a retry re-subscribes to the capturedchatCompletionStreamsource but reuses the sameemittedflag — which is precisely what makes "emitted once → never retry" hold across attempts.emittedis only flipped indoOnNext, so a failure before the first chunk still falls back as intended (the existingtestExecuteDirectStreamFallbackSuccesscovers that).
The new test (Flux.concat(just(chunk), error(...))) asserts both properties directly: the error propagates and the fallback is never invoked. One non-blocking thought: a doOnCancel/doOnTerminate hook isn't needed here, but if a future change adds buffering, the "emitted" flag would need to move to "committed to the client" — worth a short comment on the field for the next reader.
Co-authored-by: aias00 <[email protected]>
Fixes #6647.\n\nTrack whether the main stream has emitted a chunk and allow retry/fallback only before the first emission. Mid-stream failures now propagate to the client instead of replaying or mixing content.\n\nTests:
./mvnw -q -pl shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-proxy -am -DskipTests=false -Dcheckstyle.skip=false -Dtest=AiProxyExecutorServiceTest -DfailIfNoTests=false test