@@ -185,6 +185,9 @@ def __init__(
185185 self ._use_public_ip = (
186186 isinstance (address_provider , DefaultAddressProvider ) and config .use_public_ip
187187 )
188+ # asyncio tasks are weakly referenced
189+ # storing tasks here in order not to lose them midway
190+ self ._tasks = set ()
188191
189192 def add_listener (self , on_connection_opened = None , on_connection_closed = None ):
190193 """Registers a ConnectionListener.
@@ -315,22 +318,21 @@ async def on_connection_close(self, closed_connection):
315318 disconnected = False
316319 removed = False
317320 trigger_reconnection = False
318- async with self ._lock :
319- connection = self .active_connections .get (remote_uuid , None )
320- if connection == closed_connection :
321- self .active_connections .pop (remote_uuid , None )
322- removed = True
323- _logger .info (
324- "Removed connection to %s:%s, connection: %s" ,
325- remote_address ,
326- remote_uuid ,
327- connection ,
328- )
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+ )
329331
330- if not self .active_connections :
331- trigger_reconnection = True
332- if self ._client_state == ClientState .INITIALIZED_ON_CLUSTER :
333- disconnected = True
332+ if not self .active_connections :
333+ trigger_reconnection = True
334+ if self ._client_state == ClientState .INITIALIZED_ON_CLUSTER :
335+ disconnected = True
334336
335337 if disconnected :
336338 self ._lifecycle_service .fire_lifecycle_event (LifecycleState .DISCONNECTED )
@@ -813,6 +815,9 @@ def __init__(self, connection_manager, client, config, reactor, invocation_servi
813815 self ._heartbeat_timeout = config .heartbeat_timeout
814816 self ._heartbeat_interval = config .heartbeat_interval
815817 self ._heartbeat_task : asyncio .Task | None = None
818+ # asyncio tasks are weakly referenced
819+ # storing tasks here in order not to lose them midway
820+ self ._tasks = set ()
816821
817822 def start (self ):
818823 """Starts sending periodic HeartBeat operations."""
@@ -852,7 +857,9 @@ async def _check_connection(self, now, connection):
852857 if (now - connection .last_write_time ) > self ._heartbeat_interval :
853858 request = client_ping_codec .encode_request ()
854859 invocation = Invocation (request , connection = connection , urgent = True )
855- asyncio .create_task (self ._invocation_service .ainvoke (invocation ))
860+ task = asyncio .create_task (self ._invocation_service .ainvoke (invocation ))
861+ self ._tasks .add (task )
862+ task .add_done_callback (self ._tasks .discard )
856863
857864
858865_frame_header = struct .Struct ("<iH" )
0 commit comments