File tree Expand file tree Collapse file tree 2 files changed +11
-4
lines changed Expand file tree Collapse file tree 2 files changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -791,7 +791,7 @@ async def aclose(self):
791
791
async with self ._lock :
792
792
if self .connection :
793
793
await self .connection .disconnect ()
794
- self .connection .clear_connect_callbacks ( )
794
+ self .connection .deregister_connect_callback ( self . on_connect )
795
795
await self .connection_pool .release (self .connection )
796
796
self .connection = None
797
797
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