Repository navigation
Retire rejected pool connections without closing active streams - #1
Merged
Merged
Conversation
A ConnectionNotAvailable retry could reuse a stale connection indefinitely or leave an unavailable connection occupying pool capacity. Track rejected connections until outstanding requests release them, then close and replace safely in sync/async pools.
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
When a connection raises
ConnectionNotAvailable, the pool currently clears its request but can assign the same stale connection again, or retain an unavailable connection in the last pool slot. This fixes #1066 in both sync and async pools.The pool now retires connections that reject a request while still reporting availability, or are no longer referenced by an in-flight request. A retired connection is excluded from future assignments and closed only after all responses using it finish. This prevents repeated retries,
PoolTimeoutdue to a dead slot, and premature socket closure of concurrent HTTP/2 responses.#1084 also addresses encode#1066, but its immediate
close()afterConnectionNotAvailablecan interrupt another active stream using the same connection. This alternative adds deferred retirement, in-flight stream tests, and real cleanup of released stale sockets rather than closing shared sockets at rejection time.Verification
10a658221deb38a4c5b16db55ab554b0bf731707(repeated stale assignment and exhausted pool slots); 9 tests pass after the fix across sync, asyncio and Trio, including concurrent in-flight reuse..venv/bin/python -m pytest -q— 223 passed, six pre-existing XPASS..venv/bin/python scripts/unasync.py --check— passes..venv/bin/ruff check httpcore tests/test_pool_stale_connection.py— passes..venv/bin/mypy --no-warn-unused-ignores httpcore— passes (the unmodified project has an existing unused-ignore warning intrio.py).Checklist
AI assistance disclosure: implementation, tests and this PR description were prepared with GPT-6, reviewed against failing upstream behavior, the current encode#1084 alternative, and local tests.