Close `Request.stream()` generators with `aclosing`

Merged
GR
1mo ago
+44-37

Summary

_CachedRequest.wrapped_receive (used by BaseHTTPMiddleware) creates a new Request.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 in Request._get_form() can leak the same way when parsing raises (e.g. MultiPartException for too many files) or is cancelled mid-stream.

This is the leak behind:

  • #3066 โ€“ Exception ignored in: <async_generator object Request.stream ...> from uvloop.loop.Loop._asyncgen_finalizer_hook with stacked BaseHTTPMiddlewares.
  • MagicStack/uvloop#756 โ€“ the same finalizer path segfaults the interpreter under uvloop debug mode; its reproducer is two stacked BaseHTTPMiddlewares and a streaming upload.

It also provides the reproducer that #3093 was missing for the unused _wrapped_rc_stream.

Changes:

  • Wrap every internal use of self.stream() (wrapped_receive, body(), both form parsers) in contextlib.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 on GeneratorExit.
  • Drop the unused _wrapped_rc_stream.
  • Remove the filterwarnings ignore for trio's Async generator 'starlette.requests.Request.stream' was garbage collected... ResourceWarning. With the ignore removed, 21 tests fail on main; with this change the full suite passes, so the trio test runs now guard against regressions.
  • Fix tests that leaked their own async generators (unclosed receive() generators, and breaking out of request.stream()), which the stricter warning config surfaced.

User code that breaks out of async for chunk in request.stream() can still leak a generator; it may be worth a docs note recommending aclosing there as a follow-up.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

๐Ÿค– Generated with Claude Code