diff --git a/CHANGES/5259.bugfix b/CHANGES/5259.bugfix new file mode 100644 index 00000000000..c53e99e0825 --- /dev/null +++ b/CHANGES/5259.bugfix @@ -0,0 +1 @@ +Acquire the connection before running traces to prevent race condition. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 52f97c75f5a..5eea16aa6c1 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -48,6 +48,7 @@ Arthur Darcet Ben Bader Ben Timby Benedikt Reinartz +Bob Haddleton Boris Feld Boyi Chen Brett Cannon diff --git a/aiohttp/connector.py b/aiohttp/connector.py index 93b07490327..e6c36fba0d8 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -553,8 +553,14 @@ async def connect( await trace.send_connection_create_end() else: if traces: + # Acquire the connection to prevent race conditions with limits + placeholder = cast(ResponseHandler, _TransportPlaceholder()) + self._acquired.add(placeholder) + self._acquired_per_host[key].add(placeholder) for trace in traces: await trace.send_connection_reuseconn() + self._acquired.remove(placeholder) + self._drop_acquired_per_host(key, placeholder) self._acquired.add(proto) self._acquired_per_host[key].add(proto)