Repository navigation
Close the request body in ASGITransport when the app doesn't read it all - #1349
alexeyshockov wants to merge 3 commits into
Conversation
Several async iterators in httpx2 iterate another one with a bare `async for`. When their consumer stops early (`break`, or an exception between yields) and closes the outer one, the inner async generator is left suspended until the garbage collector finalizes it, on whatever event loop collects it. Trio reports that as a `ResourceWarning`. Affected: `ASGITransport` (the request body, when the app answers without reading it all), `AsyncResponseStream` (httpcore2's stream), `Response.aiter_text`, `Response.aiter_lines` and `EventSource`. Add `safe_async_iterate`, the httpx2 counterpart of httpcore2's helper, and use it at those sites and at the three that closed the inner iterator by hand.
Closing the request body with `safe_async_iterate` around the app call broke apps that leave a task in `receive()` after they return: `aclose()` on a running async generator raises `RuntimeError`, so the request failed where it used to succeed. Close the body in the existing `finally` instead, unless it is running; that task still owns it. Also test the closing in `BoundAsyncStream`, `AsyncIteratorByteStream` and an ASGI app that raises.
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
1 issue found across 8 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/httpx2/httpx2/_transports/asgi.py">
<violation number="1" location="src/httpx2/httpx2/_transports/asgi.py:177">
P2: Under Trio/AnyIO cancellation, this `finally` can run in an already-cancelled scope, so `await aclose()` may be cancelled before an async generator's awaited cleanup finishes. Shield the close with `anyio.CancelScope(shield=True)`.</violation>
</file>
Reply to a comment to ask cubic a question or push back. It learns from your replies.
View guided diff | Re-trigger cubic
| # The app may answer without reading the whole body: close it, unless a task the app left | ||
| # in `receive()` is still reading it (closing a running generator raises). | ||
| if isinstance(request_body_chunks, AsyncGeneratorType) and not request_body_chunks.ag_running: | ||
| await request_body_chunks.aclose() |
There was a problem hiding this comment.
P2: Under Trio/AnyIO cancellation, this finally can run in an already-cancelled scope, so await aclose() may be cancelled before an async generator's awaited cleanup finishes. Shield the close with anyio.CancelScope(shield=True).
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/httpx2/httpx2/_transports/asgi.py, line 177:
<comment>Under Trio/AnyIO cancellation, this `finally` can run in an already-cancelled scope, so `await aclose()` may be cancelled before an async generator's awaited cleanup finishes. Shield the close with `anyio.CancelScope(shield=True)`.</comment>
<file context>
@@ -169,6 +170,11 @@ async def send(message: MutableMapping[str, Any]) -> None:
+ # The app may answer without reading the whole body: close it, unless a task the app left
+ # in `receive()` is still reading it (closing a running generator raises).
+ if isinstance(request_body_chunks, AsyncGeneratorType) and not request_body_chunks.ag_running:
+ await request_body_chunks.aclose()
assert response_complete.is_set()
</file context>
pydantic#1212 already closes the nested response iterators (`aiter_text`, `aiter_lines`, SSE, `AsyncResponseStream`), and pydantic#1281 proposes the opposite approach for pydantic#1280. Leave those to them and keep only the `ASGITransport` request body fix, with its tests in `test_asgi.py`.
ASGITransport when the app doesn't read it all
Summary
ASGITransporttakesrequest.stream.__aiter__()and reads it from the app'sreceive(). An app that answers before the end of the body (a 413, a malformed body) leaves that async generator suspended, and nothing closes it: the garbage collector finalizes it later, on whatever event loop collects it. Under Trio that is aResourceWarning: Async generator ... was garbage collected before it had been exhausted.The body is now closed in the existing
finallyaround the app call, unless it is still running: an app may leave a task inreceive(), and closing a running generator raises.The nested response iterators (
aiter_text,aiter_lines, SSE,AsyncResponseStream) have the same problem; #1212 covers them, so they are left out here.Checklist