fix: terminate in-progress SSE event before injecting stream_read_error#1479
Open
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
Open
fix: terminate in-progress SSE event before injecting stream_read_error#1479octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
octo-patch wants to merge 1 commit intoWei-Shaw:mainfrom
Conversation
When a read error interrupts an SSE stream mid-event, sendErrorEvent() was injecting the synthetic error data line immediately after flushing the buffer without first closing the partially-written event with a blank line. SSE parsers concatenate all data lines within a single event, so the result was two JSON objects in one payload that downstream clients could not parse. Fix: write a bare newline after the initial flushBuffered() call so the in-progress event is properly terminated before the error event begins. Fixes Wei-Shaw#1471
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1471
Problem
When a read error interrupts an SSE stream mid-event (e.g.
stream_read_error),sendErrorEvent()was injecting the synthetic errordata:line immediately after flushing the buffer—without first closing the partially-written SSE event with a blank line.SSE parsers concatenate all
data:lines within a single event (i.e., between blank-line separators). So if the upstream had already sentdata: {...}\nbut the blank-line terminator had not arrived yet, the injected error event ended up appended to the same SSE event:Downstream OpenAI-compatible clients see a single event whose
datafield contains two concatenated JSON objects, causing parse failures:Could not parse message into JSONExpected ':' after property name in JSON at position 11232 (line 2 column 1)Solution
Write a bare
\n(blank line) after the initialflushBuffered()call insidesendErrorEvent(), so any in-progress SSE event is properly terminated before the error event is written. If no event is in-progress, the extra blank line is harmless—SSE parsers treat consecutive blank lines as empty event separators.The one-line change is in
backend/internal/service/openai_gateway_service.go, inside thehandleStreamingResponsefunction'ssendErrorEventclosure.Testing