Repository navigation
🐛 Fix format_sse_event dropping the event terminator when no field is set - #16265
SAURABHSALVE wants to merge 1 commit into
Conversation
…is set `format_sse_event()` built the terminating blank line by appending two empty entries to the list of field lines before joining them with `\n`. When no field was set the list was empty, so the join produced a single `"\n"` instead of the `"\n\n"` terminator the function documents. This is reachable from user code by yielding an empty `ServerSentEvent()` from an SSE *path operation*. Append the terminator to the joined field lines instead, so it is always emitted. Output for every event that sets at least one field is unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Hi! Could you please add the appropriate label to this PR so that the full set of required checks can run? Thank you! @YuriiMotov |
|
Great fix! Clear root cause analysis and clean fix for ormat_sse_event(). The string concatenation \n.join(lines) + \n\n cleanly guarantees event termination even when lines is empty. Tests cover both unit and integration levels. LGTM! |
Shriprasad-P
left a comment
There was a problem hiding this comment.
I have taken a look at the empty event scenario on master and have found that format_sse_event() is currently returning only b”\n” instead of the expected b”\n\n”. This change should fix that behaviour while still keeping the byte output for events that have data, event, id, retry or comment fields. I have also checked the whole SSE stream from start to finish with an empty ServerSentEvent() between two regular events, and everything is still framed correctly. The new regression tests that were added fail on master but pass with this change, and I have not found any problems with the formatter change.

Bug
format_sse_event()documents that:It doesn't, when no field is set. The terminator is built by appending two empty entries to the list of field lines and then joining them with
\n:When
linesis empty the join produces a single"\n"instead of"\n\n":This is reachable from user code by yielding an empty
ServerSentEvent()from an SSE path operation:The empty event is emitted as a bare
\nrather than a properly terminated (empty) event:Fix
Append the terminator to the joined field lines instead of adding it as two list entries, so it is emitted unconditionally.
Output is byte-for-byte unchanged for every event that sets at least one field (
data,event,id,retryorcomment) — the only behavior change is the previously unterminated empty event.Tests
Two tests added to
tests/test_sse.py, both of which fail onmaster:test_format_sse_event_without_fields_is_terminated— unit level.test_empty_server_sent_event_is_terminated— end to end through an SSE path operation.