Repository navigation
Close Request.stream() generators with aclosing - #3628
Merged
Merged
Conversation
`_CachedRequest.wrapped_receive` created a new `Request.stream()` generator on every call, took one chunk and abandoned it suspended, leaving it to be finalized by the GC. `body()` and the form parsers could do the same on error or cancellation. Under uvloop debug mode the asyncgen finalizer hook can segfault when run from GC (MagicStack/uvloop#756), and earlier surfaced as the traceback in Kludex#3066. Wrap every internal use of `self.stream()` in `contextlib.aclosing`, drop the unused `_wrapped_rc_stream`, and remove the pytest filter that was suppressing trio's ResourceWarning for this leak so it can't regress. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merging this PR will improve performance by 63.8%
Performance Changes
Tip Curious why performance improved? Comment Comparing |
Kludex
approved these changes
Oct 5, 2026
Owner
|
Thanks! |
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
_CachedRequest.wrapped_receive(used byBaseHTTPMiddleware) creates a newRequest.stream()async generator on every call, takes one chunk with__anext__(), and drops it while it is still suspended. Each one is then finalized by the GC through the event loop's asyncgen finalizer hook.Request.body()and the form parsers inRequest._get_form()can leak the same way when parsing raises (e.g.MultiPartExceptionfor too many files) or is cancelled mid-stream.This is the leak behind:
request.json()with three or more stackedBaseHTTPMiddleware#3066 –Exception ignored in: <async_generator object Request.stream ...>fromuvloop.loop.Loop._asyncgen_finalizer_hookwith stackedBaseHTTPMiddlewares.BaseHTTPMiddlewares and a streaming upload.It also provides the reproducer that #3093 was missing for the unused
_wrapped_rc_stream.Changes:
self.stream()(wrapped_receive,body(), both form parsers) incontextlib.aclosing. Closing a stream generator after one chunk is safe: its position lives on the request (_receive/_stream_consumed), not in the generator, and nothing runs onGeneratorExit._wrapped_rc_stream.filterwarningsignore for trio'sAsync generator 'starlette.requests.Request.stream' was garbage collected...ResourceWarning. With the ignore removed, 21 tests fail onmain; with this change the full suite passes, so the trio test runs now guard against regressions.receive()generators, andbreaking out ofrequest.stream()), which the stricter warning config surfaced.User code that
breaks out ofasync for chunk in request.stream()can still leak a generator; it may be worth a docs note recommendingaclosingthere as a follow-up.Checklist
🤖 Generated with Claude Code