fix: clean up garbage connections on cancellation in AsyncConnectionPool#983
Open
mbeijen wants to merge 1 commit into
Open
fix: clean up garbage connections on cancellation in AsyncConnectionPool#983mbeijen wants to merge 1 commit into
mbeijen wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Adds `is_connected()` to `AsyncConnectionInterface` / `ConnectionInterface`, overridden by `AsyncHTTPConnection` / `AsyncSocks5Connection` to return `False` while the TCP handshake is still in progress (i.e. while `self._connection is None`). The connection pool's `_assign_requests_to_connections()` now uses `is_connected()` (together with the set of connections currently referenced by an active pool request) to detect *garbage* connections — NEW-state connections whose request was cancelled before the TCP handshake completed. Such connections are neither closed nor idle, so the previous code silently leaked them into `self._connections`, eventually exhausting `max_connections` and causing `PoolTimeout` on every subsequent request to that pool. They are now silently dropped from the pool (there is no socket yet to close). A new test `test_connection_pool_cancellation_during_waiting_for_connection` patches `AsyncPoolRequest.wait_for_connection` to never return, then cancels the request via `anyio.move_on_after` and asserts the pool is empty afterwards. Addresses pydantic#982. Ported from httpcorexyz commit a7500e4. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
8e7fa6f to
8bfb25c
Compare
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.
Summary
AsyncConnectionPoolpoisoning bug reported in `httpcore.AsyncConnectionPool` poisoning due to `asyncio.CancelledError` #982: anasyncio.CancelledError(or any cancel scope timeout) during connection establishment leaves a NEW-state connection inself._connectionsthat is neither closed nor connected, eventually exhaustingmax_connectionsand surfacing asPoolTimeouton every subsequent request.is_connected()to(Async)ConnectionInterface, overridden on the concrete connection classes and proxy wrappers so a connection in the pre-TCP NEW state reportsFalse._assign_requests_to_connections()to recognise such garbage connections (not connected AND no live pool request references them) and silently drop them from the pool.Ported from internal commit https://codeberg.org/httpxyz/httpcorexyz/commit/a7500e4 on the
httpcorexyzforkReproduction (from #982)
Test plan
scripts/check— ruff format/check + mypy cleanscripts/test— full suite passes (1651 passed, 1 skipped) on both asyncio and trio backendsscripts/coverage— 100% coverage maintainedtest_connection_pool_cancellation_during_waiting_for_connectionverifies the pool is empty after cancellation in the connection-wait phase, on both asyncio and trioPoolTimeoutCloses #982.
🤖 Generated with Claude Code