Skip to content

Commit 6275657

Browse files
committed
Remove __del__ handler, fix pubsub weakref callback handling
1 parent 3c75ab0 commit 6275657

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

redis/asyncio/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ async def aclose(self):
791791
async with self._lock:
792792
if self.connection:
793793
await self.connection.disconnect()
794-
self.connection.clear_connect_callbacks()
794+
self.connection.deregister_connect_callback(self.on_connect)
795795
await self.connection_pool.release(self.connection)
796796
self.connection = None
797797
self.channels = {}

redis/asyncio/connection.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,15 @@ def is_connected(self):
217217
return self._reader is not None and self._writer is not None
218218

219219
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)
221223

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
224229

225230
def set_parser(self, parser_class: Type[BaseParser]) -> None:
226231
"""
@@ -263,6 +268,8 @@ async def connect(self):
263268

264269
# run any user callbacks. right now the only internal callback
265270
# 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()]
266273
for ref in self._connect_callbacks:
267274
callback = ref()
268275
task = callback(self)

0 commit comments

Comments
 (0)