fix(examples): handle slow broadcasting clients - #209
Merged
Merged
Conversation
Owner
|
@varadfromeast, thanks for bringing this up. Please rebase and incorporate some minor improvements, which I have outlined in https://github.com/sysid/sse-starlette/tree/fix/pull209. Could not force-push to your PR. |
EventSourceResponse calls aclose() on the body iterator when a data send times out. BroadcastStream had no aclose(), so a timed-out client stayed registered until its bounded queue filled and it was evicted. Add aclose() so cleanup runs on that path too. Merge the CancelledError and Exception handlers into one BaseException handler; cancellation is the normal disconnect path and must clean up like any other exit. Reword the eviction rationale: both dropping and disconnecting lose events, disconnecting just makes the gap visible. Point to event ids and Last-Event-ID as the resync mechanism this example omits. Tests: cover aclose(), add a healthy peer to the eviction test to prove other clients keep receiving, and follow the test_x_whenY_thenZ naming convention.
varadfromeast
force-pushed
the
fix/broadcasting-backpressure
branch
from
September 4, 2026 13:48
94d5270 to
cb2205e
Compare
Contributor
Author
|
@sysid Rebased onto the latest main and incorporated the improvements from fix/pull209. The update adds the send-timeout cleanup path and regression coverage; the unit suite, Ruff checks, and type checks pass. Could you please review when you have a chance? |
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
EventSourceResponsecancels the iterator on disconnectRoot cause
The example used
asyncio.Queue()with the defaultmaxsize=0, so itsQueueFullbranch was unreachable. Simply bounding the queue was not sufficient: removing a full queue from the broadcaster did not terminate the corresponding stream, which could later wait forever for another event. In addition,asyncio.CancelledErroris not caught byexcept Exception, so normal response cancellation could leave the client registered.The example now uses a bounded queue with an explicit disconnect policy. When a queue fills, buffered events are discarded and a sentinel terminates the evicted stream immediately; other clients continue receiving events through
put_nowait().Verification
make test-unit— 81 passed, 2 deselectedmake tyuv run ruff check examples/02_broadcasting.py tests/test_broadcasting_example.pyuv run ruff format --check examples/02_broadcasting.py tests/test_broadcasting_example.py