@@ -82,51 +82,54 @@ async def register_listener(
8282 tg .create_task (task )
8383 return registration_id
8484 except Exception :
85- await self .deregister_listener (registration_id )
85+ await self ._deregister_listener_unsafe (registration_id )
8686 raise HazelcastError ("Listener cannot be added" )
8787
8888 async def deregister_listener (self , user_registration_id ):
8989 check_not_none (user_registration_id , "None user_registration_id is not allowed!" )
9090 async with self ._registration_lock :
91- listener_registration = self ._active_registrations .pop (user_registration_id , None )
92- if not listener_registration :
93- return False
94-
95- async def handle (inv : Invocation , conn : AsyncioConnection ):
96- try :
97- await inv .future
98- except Exception as e :
99- if not isinstance (
100- e , (HazelcastClientNotActiveError , IOError , TargetDisconnectedError )
101- ):
102- _logger .warning (
103- "Deregistration of listener with ID %s has failed for address %s: %s" ,
104- user_registration_id ,
105- conn .remote_address ,
106- e ,
107- )
91+ return await self ._deregister_listener_unsafe (user_registration_id )
10892
109- async with asyncio .TaskGroup () as tg :
110- items = listener_registration .connection_registrations .items ()
111- for connection , event_registration in items :
112- # Remove local handler
113- self .remove_event_handler (event_registration .correlation_id )
114- # The rest is for deleting the remote registration
115- server_registration_id = event_registration .server_registration_id
116- deregister_request = listener_registration .encode_deregister_request (
117- server_registration_id
118- )
119- if deregister_request is None :
120- # None means no remote registration (e.g. for backup acks)
121- continue
122- invocation = Invocation (
123- deregister_request , connection = connection , timeout = sys .maxsize , urgent = True
93+ async def _deregister_listener_unsafe (self , user_registration_id ):
94+ listener_registration = self ._active_registrations .pop (user_registration_id , None )
95+ if not listener_registration :
96+ return False
97+
98+ async def handle (inv : Invocation , conn : AsyncioConnection ):
99+ try :
100+ await inv .future
101+ except Exception as e :
102+ if not isinstance (
103+ e , (HazelcastClientNotActiveError , IOError , TargetDisconnectedError )
104+ ):
105+ _logger .warning (
106+ "Deregistration of listener with ID %s has failed for address %s: %s" ,
107+ user_registration_id ,
108+ conn .remote_address ,
109+ e ,
124110 )
125- self ._invocation_service .invoke (invocation )
126- tg .create_task (handle (invocation , connection ))
127111
128- listener_registration .connection_registrations .clear ()
129- return True
112+ async with asyncio .TaskGroup () as tg :
113+ items = listener_registration .connection_registrations .items ()
114+ for connection , event_registration in items :
115+ # Remove local handler
116+ self .remove_event_handler (event_registration .correlation_id )
117+ # The rest is for deleting the remote registration
118+ server_registration_id = event_registration .server_registration_id
119+ deregister_request = listener_registration .encode_deregister_request (
120+ server_registration_id
121+ )
122+ if deregister_request is None :
123+ # None means no remote registration (e.g. for backup acks)
124+ continue
125+ invocation = Invocation (
126+ deregister_request , connection = connection , timeout = sys .maxsize , urgent = True
127+ )
128+ self ._invocation_service .invoke (invocation )
129+ tg .create_task (handle (invocation , connection ))
130+
131+ listener_registration .connection_registrations .clear ()
132+ return True
130133
131134 def handle_client_message (self , message : InboundMessage , correlation_id : int ):
132135 handler = self ._event_handlers .get (correlation_id , None )
0 commit comments