Fix: Connection pool should not close connections assigned to queued requests - #1114
Closed
nishant1000 wants to merge 1 commit into
Closed
nishant1000 wants to merge 1 commit into
nishant1000 wants to merge 1 commit into
Conversation
…requests (encode#1110) The race condition occurs when: 1. Thread A assigns an idle connection to a queued PoolRequest 2. Thread B's _assign_requests_to_connections() sees it as idle and closes it 3. Thread A's handle_request() fails on the now-closed connection Fix: In the surplus idle connection cleanup loop, skip connections that have been assigned to any request in the pool. Added _is_connection_assigned() helper method to both sync and async connection 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
Fixes #1110
Problem
There is a race condition in the connection pool where _assign_requests_to_connections() can close a connection that has already been assigned to a queued request.
The race condition:
Fix
In the surplus idle connection cleanup loop within _assign_requests_to_connections(), skip connections that have been assigned to any request in the pool.
Added _is_connection_assigned() helper method to both ConnectionPool (sync) and AsyncConnectionPool (async) that checks whether a connection is referenced by any request in the pool's request list.
Changes
Testing
All 12 new tests pass. All existing sync connection pool tests pass (4 failures are pre-existing: 3 missing h2 module, 1 keepalive_expiry timing).