Unwind transports in reverse order in `AsyncClient`

KL
1mo ago
+42-19

AsyncClient now unwinds its transports with an AsyncExitStack.

  • Transports are exited and closed in reverse order of entry. A transport that holds a task group, such as ASGIWebSocketTransport, must exit before anything entered earlier, or AnyIO and Trio raise cancel-scope and nursery errors.
  • A transport that raises on exit or close no longer skips closing the others.
async with httpx2.AsyncClient(transport=transport, mounts={"http://a": mounted}):
    ...
# `mounted` exits first, then `transport`, even if one of them raises

This also unblocks #1269, which gives ASGITransport its own task group.

AI Disclaimer

This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.