Stream server SSE events live instead of buffering until stream close - #1339
Merged
Conversation
SSEStream.body_read! delegated to readbytes!(buffer, dst, length(dst)), which on a Base.BufferStream blocks until the destination is completely filled or the stream closes. The server response writers read bodies with a 16KB buffer, so every event written to an SSE response sat in the BufferStream until the handler finished and the stream closed — clients received the whole stream as a single burst (reproducible with the sse_stream docstring example + curl -N), making long-lived notification streams unusable. body_read! now blocks only until at least one byte is buffered (via eof) and returns what is immediately available, so each written event reaches the transport promptly on both the HTTP/1 and HTTP/2 server write paths. The new test drives the producer from the client callback: the second event is only written after the client has observed the first, so the test fails deterministically (with a sentinel event, no wall-clock assertions) if the transport buffers until close. Discovered implementing MCP 2026-07-28 subscriptions/listen in ModelContextProtocol.jl, which needs long-lived server-to-client notification streams. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Write each body chunk as soon as body_read! returns it. Finish body-only responses with an empty END_STREAM DATA frame so trailers keep their existing terminal HEADERS behavior. This prevents long-lived SSE responses, including MCP subscription streams, from holding the first event until the next event or EOF. Add a real HTTP/2 regression test and keep the HTTP/1 test synchronized without atomics.
quinnj
force-pushed
the
fix-server-sse-streaming
branch
from
July 29, 2026 19:53
289b842 to
611ab59
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1339 +/- ##
=======================================
Coverage 88.30% 88.30%
=======================================
Files 30 30
Lines 11921 11916 -5
=======================================
- Hits 10527 10523 -4
+ Misses 1394 1393 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
BufferStream.eof already waits until data is available or the stream is closed and drained. Read the available bytes directly after that wait and avoid an unreachable loop backedge.
Make reset and connection-close error mapping deterministic. This removes coverage dependence on cancellation race timing.
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
Root cause
SSEStream.body_read! used a BufferStream read that waited for the 16 KiB destination to fill or for the stream to close. This buffered HTTP/1 events until close.
HTTP/2 had a second delay. Its AbstractBody writer retained one complete chunk so it could mark that chunk END_STREAM later. A quiet subscription therefore held its first event until a second event arrived or the producer closed. For MCP, this could hide the required subscription acknowledgement indefinitely.
Fix
Validation
Initial change prepared with Claude Code.
Co-authored by Codex