@@ -187,6 +187,7 @@ def __init__(
187187 )
188188 # asyncio tasks are weakly referenced
189189 # storing tasks here in order not to lose them midway
190+ # see: https: // docs.python.org / 3 / library / asyncio - task.html # creating-tasks
190191 self ._tasks = set ()
191192
192193 def add_listener (self , on_connection_opened = None , on_connection_closed = None ):
@@ -318,21 +319,22 @@ async def on_connection_close(self, closed_connection):
318319 disconnected = False
319320 removed = False
320321 trigger_reconnection = False
321- connection = self .active_connections .get (remote_uuid , None )
322- if connection == closed_connection :
323- self .active_connections .pop (remote_uuid , None )
324- removed = True
325- _logger .info (
326- "Removed connection to %s:%s, connection: %s" ,
327- remote_address ,
328- remote_uuid ,
329- connection ,
330- )
322+ async with self ._lock :
323+ connection = self .active_connections .get (remote_uuid , None )
324+ if connection == closed_connection :
325+ self .active_connections .pop (remote_uuid , None )
326+ removed = True
327+ _logger .info (
328+ "Removed connection to %s:%s, connection: %s" ,
329+ remote_address ,
330+ remote_uuid ,
331+ connection ,
332+ )
331333
332- if not self .active_connections :
333- trigger_reconnection = True
334- if self ._client_state == ClientState .INITIALIZED_ON_CLUSTER :
335- disconnected = True
334+ if not self .active_connections :
335+ trigger_reconnection = True
336+ if self ._client_state == ClientState .INITIALIZED_ON_CLUSTER :
337+ disconnected = True
336338
337339 if disconnected :
338340 self ._lifecycle_service .fire_lifecycle_event (LifecycleState .DISCONNECTED )
@@ -813,6 +815,7 @@ def __init__(self, connection_manager, client, config, reactor, invocation_servi
813815 self ._heartbeat_task : asyncio .Task | None = None
814816 # asyncio tasks are weakly referenced
815817 # storing tasks here in order not to lose them midway
818+ # see: https: // docs.python.org / 3 / library / asyncio - task.html # creating-tasks
816819 self ._tasks = set ()
817820
818821 def start (self ):
0 commit comments