Repository navigation
Cancel blocked server-stream handlers on client disconnect - #395
Open
stefanvanburen wants to merge 3 commits into
Open
stefanvanburen wants to merge 3 commits into
stefanvanburen wants to merge 3 commits into
Conversation
The disconnect watcher added in #175 only set an event that the response loop checked between messages, so a handler blocked before producing its next message (such as a health `Watch` waiting for a status change) kept running until it next yielded or its deadline passed. Run the response loop inside an `asyncio.timeout(None)` scope that the watcher expires on `http.disconnect`. The cancellation interrupts whatever the loop awaits, including the handler, so its generator and any wrapping interceptors unwind with their `finally` blocks, and the RPC still ends as CANCELED with leading metadata interceptors seeing that error in `on_end`. Signed-off-by: Stefan VanBuren <stefan@vanburen.xyz>
Both server-stream disconnect tests in `test_roundtrip.py` built the same scope inline. Also leave the new test's metadata interceptor untyped, like the one in `test_interceptor.py`. Signed-off-by: Stefan VanBuren <stefan@vanburen.xyz>
Signed-off-by: Stefan VanBuren <stefan@vanburen.xyz>
stefanvanburen
marked this pull request as ready for review
October 14, 2026 00:28
anuraaga
approved these changes
Oct 14, 2026
Member
Author
agreed; this one was particularly dense. although tbh, the code itself is really quite subtle, at least to me. :) do you mean you'd like to land those first? Happy to hold off until we get those situated given that these are all in roughly the same area of the codebase! |
This branch has not been deployed
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.
#175 made the server watch
receive()forhttp.disconnectduring server streams, but the response loop only checks for it between messages, so a handler that's waiting to produce its next message (a healthWatchthat only yields on status changes, say) keeps running until it next yields or hits its deadline. This runs the response loop inside anasyncio.timeout(None)scope that the watcher reschedules to now on disconnect, which cancels whatever the loop is awaiting, the handler'sanextor asend, and ends the call as CANCELED as before. The handler's and stream interceptors'finallyblocks run andon_endsees CANCELED.Bidi and client streams have the same gap once the request body ends, but there the handler is the one reading
receive(), so a separate watcher would steal body chunks. Covering them needs a single task that ownsreceive()for the whole call, which I'd rather do as a follow-up, especially since #348 rewrites this code for trio. This maps onto itswatch_for_disconnect; under trio the scope would be aCancelScope.