File tree 2 files changed +13
-6
lines changed 2 files changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -690,7 +690,7 @@ def __del__(self):
690
690
def reset (self ):
691
691
if self .connection :
692
692
self .connection .disconnect ()
693
- self .connection .clear_connect_callbacks ( )
693
+ self .connection ._deregister_connect_callback ( self . on_connect )
694
694
self .connection_pool .release (self .connection )
695
695
self .connection = None
696
696
self .health_check_response_counter = 0
@@ -748,7 +748,7 @@ def execute_command(self, *args):
748
748
)
749
749
# register a callback that re-subscribes to any channels we
750
750
# were listening to when we were disconnected
751
- self .connection .register_connect_callback (self .on_connect )
751
+ self .connection ._register_connect_callback (self .on_connect )
752
752
if self .push_handler_func is not None and not HIREDIS_AVAILABLE :
753
753
self .connection ._parser .set_push_handler (self .push_handler_func )
754
754
connection = self .connection
Original file line number Diff line number Diff line change @@ -237,11 +237,16 @@ def _construct_command_packer(self, packer):
237
237
else :
238
238
return PythonRespSerializer (self ._buffer_cutoff , self .encoder .encode )
239
239
240
- def register_connect_callback (self , callback ):
241
- self ._connect_callbacks .append (weakref .WeakMethod (callback ))
240
+ def _register_connect_callback (self , callback ):
241
+ wm = weakref .WeakMethod (callback )
242
+ if wm not in self ._connect_callbacks :
243
+ self ._connect_callbacks .append (wm )
242
244
243
- def clear_connect_callbacks (self ):
244
- self ._connect_callbacks = []
245
+ def _deregister_connect_callback (self , callback ):
246
+ try :
247
+ self ._connect_callbacks .remove (weakref .WeakMethod (callback ))
248
+ except ValueError :
249
+ pass
245
250
246
251
def set_parser (self , parser_class ):
247
252
"""
@@ -279,6 +284,8 @@ def connect(self):
279
284
280
285
# run any user callbacks. right now the only internal callback
281
286
# is for pubsub channel/pattern resubscription
287
+ # first, remove any dead weakrefs
288
+ self ._connect_callbacks = [ref for ref in self ._connect_callbacks if ref ()]
282
289
for ref in self ._connect_callbacks :
283
290
callback = ref ()
284
291
if callback :
You can’t perform that action at this time.
0 commit comments