Make AIOHTTPTransport.connect() reentrant for shared client instances - #612
Closed
magicmark wants to merge 2 commits into
Closed
Make AIOHTTPTransport.connect() reentrant for shared client instances#612magicmark wants to merge 2 commits into
magicmark wants to merge 2 commits into
Conversation
Demonstrates that AIOHTTPTransport.connect() raises TransportAlreadyConnected when two async-with blocks enter the same Client concurrently — a common pattern when the Client is cached (e.g. via lru_cache) for connection pooling. Co-Authored-By: Claude <noreply@anthropic.com>
A cached Client instance (common when wrapping with lru_cache for connection pooling) would raise TransportAlreadyConnected if entered concurrently from multiple async-with blocks, or if a prior session wasn't cleaned up due to an abrupt disconnection. connect() now reuses the existing aiohttp.ClientSession when one is already active (incrementing a reference count), and close() only tears it down when the last caller exits. This matches aiohttp's own recommendation of a single long-lived session per application. Co-Authored-By: Claude <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #612 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 38 40 +2
Lines 2908 3333 +425
==========================================
+ Hits 2908 3333 +425 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
When a
Clientis cached and reused across requests (e.g. wrapped inlru_cachefor connection pooling, as aiohttp recommends), concurrentasync with clientblocks race ontransport.connect()— the second caller hitsTransportAlreadyConnectedbecause the session from the first caller is still active.This also bites when a downstream consumer disconnects abruptly (e.g. a mobile client crashes mid-stream):
__aexit__may never fire, the session leaks, and the next request on the same transport fails permanently.The fix adds a reference count to
connect()/close(). If the session already exists,connect()reuses it instead of raising.close()only tears it down when the last caller exits. This matches aiohttp's own guidance that a singleClientSessionshould be long-lived and shared.The first commit adds an xfail'd minimal repro (two concurrent
asyncio.gathercallers on the same Client). The second commit fixes the behavior and un-xfails it.Draft PR: This PR was sent by Claude and may not have yet been reviewed by mark.