Skip to content

fix: Fixed potential deadlock from unexpected __del__ call #3654

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
May 26, 2025
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
5 changes: 5 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ def __init__(
]:
raise RedisError("Client caching is only supported with RESP version 3")

# TODO: To avoid breaking changes during the bug fix, we have to keep non-reentrant lock.
# TODO: Remove this before next major version (7.0.0)
self.single_connection_lock = threading.Lock()
self.connection = None
self._single_connection_client = single_connection_client
Expand Down Expand Up @@ -774,6 +776,9 @@ def __init__(
self._event_dispatcher = EventDispatcher()
else:
self._event_dispatcher = event_dispatcher

# TODO: To avoid breaking changes during the bug fix, we have to keep non-reentrant lock.
# TODO: Remove this before next major version (7.0.0)
self._lock = threading.Lock()
if self.encoder is None:
self.encoder = self.connection_pool.get_encoder()
Expand Down
6 changes: 3 additions & 3 deletions redis/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def __init__(
self.result_callbacks = CaseInsensitiveDict(self.__class__.RESULT_CALLBACKS)

self.commands_parser = CommandsParser(self)
self._lock = threading.Lock()
self._lock = threading.RLock()

def __enter__(self):
return self
Expand Down Expand Up @@ -1474,7 +1474,7 @@ def __init__(
self.connection_kwargs = kwargs
self.read_load_balancer = LoadBalancer()
if lock is None:
lock = threading.Lock()
lock = threading.RLock()
self._lock = lock
if event_dispatcher is None:
self._event_dispatcher = EventDispatcher()
Expand Down Expand Up @@ -2178,7 +2178,7 @@ def __init__(
kwargs.get("decode_responses", False),
)
if lock is None:
lock = threading.Lock()
lock = threading.RLock()
self._lock = lock
self.parent_execute_command = super().execute_command
self._execution_strategy: ExecutionStrategy = (
Expand Down
14 changes: 11 additions & 3 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ def __init__(
self.credential_provider = conn.credential_provider
self._pool_lock = pool_lock
self._cache = cache
self._cache_lock = threading.Lock()
self._cache_lock = threading.RLock()
self._current_command_cache_key = None
self._current_options = None
self.register_connect_callback(self._enable_tracking_callback)
Expand Down Expand Up @@ -1420,8 +1420,16 @@ def __init__(
# object of this pool. subsequent threads acquiring this lock
# will notice the first thread already did the work and simply
# release the lock.
self._fork_lock = threading.Lock()
self._lock = threading.Lock()

self._fork_lock = threading.RLock()

if self.cache is None:
self._lock = threading.RLock()
else:
# TODO: To avoid breaking changes during the bug fix, we have to keep non-reentrant lock.
# TODO: Remove this before next major version (7.0.0)
self._lock = threading.Lock()

self.reset()

def __repr__(self) -> (str, str):
Expand Down
Loading