fix(ssestream): skip dispatching empty events for comment-only SSE blocks#643
Open
jh317729530 wants to merge 1 commit into
Open
fix(ssestream): skip dispatching empty events for comment-only SSE blocks#643jh317729530 wants to merge 1 commit into
jh317729530 wants to merge 1 commit into
Conversation
…ocks Per the SSE specification, if the data buffer is empty and the event type is empty when an empty line is encountered, the event should be ignored rather than dispatched. Previously, the decoder dispatched an event unconditionally on every empty line, which caused downstream json.Unmarshal errors when SSE keep-alive comments like ": ping" were followed by blank lines. This change adds a check so that comment-only blocks (e.g. ": ping\n\n") no longer produce spurious empty events, while still correctly dispatching events that have either data or an explicit event type set. Made-with: Cursor
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.
Summary
The
eventStreamDecoder.Next()method currently dispatches an event unconditionally whenever it encounters an empty line. This causes issues when SSE keep-alive comments (e.g.: ping) are followed by a blank line — an emptyEvent(with no type and no data) is dispatched, which leads to ajson.Unmarshalerror (unexpected end of JSON input) in the upstreamStream[T].Next().Per the SSE specification:
In other words, if no
data:orevent:fields were set before the blank line, the event should be silently discarded.Changes
packages/ssestream/ssestream.go: Added a guard ineventStreamDecoder.Next()tocontinue(instead of dispatching) when an empty line is encountered but bothdataandeventare empty.packages/ssestream/ssestream_test.go: Added comprehensive tests covering:: ping\n\n) produce no eventsReproduction
Send an SSE stream containing:
Before fix: The
: ping\n\nblock dispatches an empty event →json.Unmarshal([]byte{}, ...)→ error.After fix: The
: ping\n\nblock is silently skipped, only thedata: {"id":"1"}event is dispatched.Test plan
go test ./packages/ssestream/ -v)Made with Cursor