Skip to content

Commit

Permalink
Fix for race condition on connections in BaseConnector (#4937)
Browse files Browse the repository at this point in the history
(cherry picked from commit ad00c2e)
  • Loading branch information
PandaWill authored and Patchback committed Oct 15, 2020
1 parent c1679a8 commit 9e1c95a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 3 deletions.
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 @@ -269,6 +269,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 @@ -474,8 +474,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 @@ -1713,7 +1713,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 @@ -1726,6 +1726,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

0 comments on commit 9e1c95a

Please sign in to comment.