File tree Expand file tree Collapse file tree 2 files changed +11
-8
lines changed Expand file tree Collapse file tree 2 files changed +11
-8
lines changed Original file line number Diff line number Diff line change @@ -720,15 +720,11 @@ async def __aenter__(self):
720
720
async def __aexit__ (self , exc_type , exc_value , traceback ):
721
721
await self .reset ()
722
722
723
- def __del__ (self ):
724
- if self .connection :
725
- self .connection .clear_connect_callbacks ()
726
-
727
723
async def reset (self ):
728
724
async with self ._lock :
729
725
if self .connection :
730
726
await self .connection .disconnect ()
731
- self .connection .clear_connect_callbacks ( )
727
+ self .connection .deregister_connect_callback ( self . on_connect )
732
728
await self .connection_pool .release (self .connection )
733
729
self .connection = None
734
730
self .channels = {}
Original file line number Diff line number Diff line change @@ -216,10 +216,15 @@ def is_connected(self):
216
216
return self ._reader is not None and self ._writer is not None
217
217
218
218
def register_connect_callback (self , callback ):
219
- self ._connect_callbacks .append (weakref .WeakMethod (callback ))
219
+ wm = weakref .WeakMethod (callback )
220
+ if wm not in self ._connect_callbacks :
221
+ self ._connect_callbacks .append (wm )
220
222
221
- def clear_connect_callbacks (self ):
222
- self ._connect_callbacks = []
223
+ def deregister_connect_callback (self , callback ):
224
+ try :
225
+ self ._connect_callbacks .remove (weakref .WeakMethod (callback ))
226
+ except ValueError :
227
+ pass
223
228
224
229
def set_parser (self , parser_class : Type [BaseParser ]) -> None :
225
230
"""
@@ -262,6 +267,8 @@ async def connect(self):
262
267
263
268
# run any user callbacks. right now the only internal callback
264
269
# is for pubsub channel/pattern resubscription
270
+ # first, remove any dead weakrefs
271
+ self ._connect_callbacks = [ref for ref in self ._connect_callbacks if ref ()]
265
272
for ref in self ._connect_callbacks :
266
273
callback = ref ()
267
274
task = callback (self )
You can’t perform that action at this time.
0 commit comments