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 @@ -771,15 +771,11 @@ async def __aenter__(self):
771
771
async def __aexit__ (self , exc_type , exc_value , traceback ):
772
772
await self .reset ()
773
773
774
- def __del__ (self ):
775
- if self .connection :
776
- self .connection .clear_connect_callbacks ()
777
-
778
774
async def reset (self ):
779
775
async with self ._lock :
780
776
if self .connection :
781
777
await self .connection .disconnect ()
782
- self .connection .clear_connect_callbacks ( )
778
+ self .connection .deregister_connect_callback ( self . on_connect )
783
779
await self .connection_pool .release (self .connection )
784
780
self .connection = None
785
781
self .channels = {}
Original file line number Diff line number Diff line change @@ -217,10 +217,15 @@ def is_connected(self):
217
217
return self ._reader is not None and self ._writer is not None
218
218
219
219
def register_connect_callback (self , callback ):
220
- self ._connect_callbacks .append (weakref .WeakMethod (callback ))
220
+ wm = weakref .WeakMethod (callback )
221
+ if wm not in self ._connect_callbacks :
222
+ self ._connect_callbacks .append (wm )
221
223
222
- def clear_connect_callbacks (self ):
223
- self ._connect_callbacks = []
224
+ def deregister_connect_callback (self , callback ):
225
+ try :
226
+ self ._connect_callbacks .remove (weakref .WeakMethod (callback ))
227
+ except ValueError :
228
+ pass
224
229
225
230
def set_parser (self , parser_class : Type [BaseParser ]) -> None :
226
231
"""
@@ -263,6 +268,8 @@ async def connect(self):
263
268
264
269
# run any user callbacks. right now the only internal callback
265
270
# is for pubsub channel/pattern resubscription
271
+ # first, remove any dead weakrefs
272
+ self ._connect_callbacks = [ref for ref in self ._connect_callbacks if ref ()]
266
273
for ref in self ._connect_callbacks :
267
274
callback = ref ()
268
275
task = callback (self )
You can’t perform that action at this time.
0 commit comments