Skip to content

Close the request body in ASGITransport when the app doesn't read it all - #1349

Open
alexeyshockov wants to merge 3 commits into
pydantic:mainfrom
alexeyshockov:fix/close-inner-async-iterators
Open

alexeyshockov wants to merge 3 commits into
pydantic:mainfrom
alexeyshockov:fix/close-inner-async-iterators

Conversation

@alexeyshockov

@alexeyshockov alexeyshockov commented Nov 16, 2026 •

Copy link
Copy Markdown

Summary

ASGITransport takes request.stream.__aiter__() and reads it from the app's receive(). 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 a ResourceWarning: Async generator ... was garbage collected before it had been exhausted.

The body is now closed in the existing finally around the app call, unless it is still running: an app may leave a task in receive(), 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

  • 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.

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.
@veria-ai

veria-ai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@codspeed

codspeed Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 18 untouched benchmarks
⏩ 7 skipped benchmarks1


Comparing alexeyshockov:fix/close-inner-async-iterators (fb2d98d) with main (de59b26)

Open in CodSpeed

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread src/httpx2/httpx2/_utils.py Resolved
# 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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Comment thread src/httpx2/httpx2/_utils.py Resolved
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`.
@alexeyshockov alexeyshockov changed the title Close inner async iterators when the consumer stops early Close the request body in ASGITransport when the app doesn't read it all Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant