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 @@ -730,15 +730,11 @@ async def __aenter__(self):
730
730
async def __aexit__ (self , exc_type , exc_value , traceback ):
731
731
await self .reset ()
732
732
733
- def __del__ (self ):
734
- if self .connection :
735
- self .connection .clear_connect_callbacks ()
736
-
737
733
async def reset (self ):
738
734
async with self ._lock :
739
735
if self .connection :
740
736
await self .connection .disconnect ()
741
- self .connection .clear_connect_callbacks ( )
737
+ self .connection .deregister_connect_callback ( self . on_connect )
742
738
await self .connection_pool .release (self .connection )
743
739
self .connection = None
744
740
self .channels = {}
Original file line number Diff line number Diff line change @@ -222,10 +222,15 @@ def is_connected(self):
222
222
return self ._reader is not None and self ._writer is not None
223
223
224
224
def register_connect_callback (self , callback ):
225
- self ._connect_callbacks .append (weakref .WeakMethod (callback ))
225
+ wm = weakref .WeakMethod (callback )
226
+ if wm not in self ._connect_callbacks :
227
+ self ._connect_callbacks .append (wm )
226
228
227
- def clear_connect_callbacks (self ):
228
- self ._connect_callbacks = []
229
+ def deregister_connect_callback (self , callback ):
230
+ try :
231
+ self ._connect_callbacks .remove (weakref .WeakMethod (callback ))
232
+ except ValueError :
233
+ pass
229
234
230
235
def set_parser (self , parser_class : Type [BaseParser ]) -> None :
231
236
"""
@@ -268,6 +273,8 @@ async def connect(self):
268
273
269
274
# run any user callbacks. right now the only internal callback
270
275
# is for pubsub channel/pattern resubscription
276
+ # first, remove any dead weakrefs
277
+ self ._connect_callbacks = [ref for ref in self ._connect_callbacks if ref ()]
271
278
for ref in self ._connect_callbacks :
272
279
callback = ref ()
273
280
task = callback (self )
You can’t perform that action at this time.
0 commit comments