Skip to content

Stream server SSE events live instead of buffering until stream close - #1339

Merged
quinnj merged 5 commits into
masterfrom
fix-server-sse-streaming
Jul 29, 2026
Merged

Stream server SSE events live instead of buffering until stream close#1339
quinnj merged 5 commits into
masterfrom
fix-server-sse-streaming

Conversation

@quinnj

@quinnj quinnj commented Jul 29, 2026

Copy link
Copy Markdown
Member

Summary

  • Stream HTTP/1 SSE bytes as soon as they are available.
  • Stream HTTP/2 AbstractBody chunks as soon as each read completes.
  • Restore live delivery for long-lived SSE responses such as MCP 2026-07-28 subscriptions/listen.

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

  • Wait for at least one SSE byte, then return all bytes that are immediately available.
  • Send every non-empty HTTP/2 body chunk immediately.
  • At body EOF, send an empty END_STREAM DATA frame when there are no trailers.
  • Keep trailing HEADERS as the terminal frame when trailers are present.
  • Add deterministic HTTP/1 and HTTP/2 client/producer handshake tests. The producer writes event two only after the client receives event one.

Validation

  • Rebased onto current master at e7fbc1f.
  • Full package test suite passed on Julia 1.12.6, including all 63 trim-compile checks.
  • Complete HTTP/1 and HTTP/2 server test files passed on Julia 1.10.11 and 1.11.9.
  • Live ModelContextProtocol.jl subscriptions/listen smoke passed: acknowledgement and later list-changed notification both arrived before stream close.
  • HTTP/2 response trailers and a 40,008-byte SSE drain passed direct checks.

Initial change prepared with Claude Code.

Co-authored by Codex

quinnj and others added 2 commits July 29, 2026 13:29
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
quinnj force-pushed the fix-server-sse-streaming branch from 289b842 to 611ab59 Compare July 29, 2026 19:53
@codecov

codecov Bot commented Jul 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.30%. Comparing base (e7fbc1f) to head (1677743).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

quinnj added 3 commits July 29, 2026 14:05
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.
@quinnj
quinnj merged commit ed8303c into master Jul 29, 2026
8 checks passed
@quinnj
quinnj deleted the fix-server-sse-streaming branch July 29, 2026 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant