Skip to content

Fix garbage collection deadlock #1578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
flake8>=3.9.2
pytest==6.2.5
pytest-timeout==2.0.1
tox==3.24.4
tox-docker==3.1.0
invoke==1.6.0
Expand Down
9 changes: 6 additions & 3 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import socket
import threading
import warnings
import weakref

from redis.exceptions import (
AuthenticationError,
Expand Down Expand Up @@ -562,7 +563,7 @@ def __del__(self):
pass

def register_connect_callback(self, callback):
self._connect_callbacks.append(callback)
self._connect_callbacks.append(weakref.WeakMethod(callback))

def clear_connect_callbacks(self):
self._connect_callbacks = []
Expand All @@ -588,8 +589,10 @@ def connect(self):

# run any user callbacks. right now the only internal callback
# is for pubsub channel/pattern resubscription
for callback in self._connect_callbacks:
callback(self)
for ref in self._connect_callbacks:
callback = ref()
if callback:
callback(self)

def _connect(self):
"Create a TCP socket connection"
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,15 @@ def exception_handler(ex, pubsub, thread):
assert event.wait(timeout=1.0)
pubsub_thread.join(timeout=1.0)
assert not pubsub_thread.is_alive()


class TestPubSubDeadlock:
@pytest.mark.timeout(30, method='thread')
def test_pubsub_deadlock(self, master_host):
pool = redis.ConnectionPool(host=master_host)
r = redis.Redis(connection_pool=pool)

for i in range(60):
p = r.pubsub()
p.subscribe("my-channel-1", "my-channel-2")
pool.reset()