Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for race condition on connections in BaseConnector #4937

Merged
merged 6 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/4936.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix for race condition on connections in BaseConnector that leads to exceeding the connection limit.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ Weiwei Wang
Will McGugan
Willem de Groot
William Grzybowski
William S.
Wilson Ong
Yang Zhou
Yannick Koechlin
Expand Down
5 changes: 3 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,9 @@ async def connect(self, req: 'ClientRequest',
key = req.connection_key
available = self._available_connections(key)

# Wait if there are no available connections.
if available <= 0:
# Wait if there are no available connections or if there are/were
# waiters (i.e. don't steal connection from a waiter about to wake up)
if available <= 0 or key in self._waiters:
fut = self._loop.create_future()

# This connection will now count towards the limit.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ async def create_connection(req, traces, timeout):
# with multiple concurrent requests and stops when it hits a
# predefined maximum number of requests.

max_requests = 10
max_requests = 50
num_requests = 0
start_requests = max_connections + 1

Expand All @@ -1712,6 +1712,7 @@ async def f(start=True):
connection = await conn.connect(req, None, ClientTimeout())
await asyncio.sleep(0)
connection.release()
await asyncio.sleep(0)
tasks = [
loop.create_task(f(start=False))
for i in range(start_requests)
Expand Down