-
Notifications
You must be signed in to change notification settings - Fork 2.7k
SentinelConnectionPool should retrieve updated master address from Sentinel on connection retries #3875
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
base: master
Are you sure you want to change the base?
Conversation
|
Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset. In case there are security findings, they will be communicated to you as a comment inside the PR. Hope you’ll enjoy using Jit. Questions? Comments? Want to learn more? Get in touch with us. |
|
Hi @SamuelGD, than you for your contribution! We will review it soon. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR refactors how SentinelManagedConnection handles connection retries to ensure that the master address is retrieved from Sentinel on each retry attempt. Previously, the connection retry logic was handled by a custom connect() method that didn't fetch fresh master addresses during retries.
Key changes:
- Refactored
SentinelManagedConnection._connect()to fetch master/slave addresses directly and rely on parent class retry logic - Removed custom
connect(),connect_to(), and_connect_retry()methods in favor of overriding_connect() - Added ConnectionError exception handling in async connection flow to preserve custom connection errors
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
redis/sentinel.py |
Refactored SentinelManagedConnection._connect() to fetch master address on each call, removing custom retry wrapper |
redis/asyncio/sentinel.py |
Async version of the _connect() refactoring with equivalent logic |
redis/asyncio/connection.py |
Added explicit ConnectionError catch-and-rethrow to preserve custom connection errors |
tests/test_sentinel_managed_connection.py |
Updated tests to mock parent class _connect() and added new test for connect_check_health() retry behavior |
tests/test_asyncio/test_sentinel_managed_connection.py |
Async version of the test updates with equivalent assertions |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
redis/sentinel.py
Outdated
| def _connect(self): | ||
| if self._sock: | ||
| return # already connected | ||
| return super()._connect() # already connected |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic here is incorrect. When self._sock exists, the connection is already established and the method should simply return without calling super()._connect(). The parent Connection._connect() returns a socket object, which would be inconsistent with the rest of this method that doesn't return anything. This should be:
if self._sock:
return # already connected| return super()._connect() # already connected | |
| return # already connected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the connection is already established, _connect is never called https://github.com/redis/redis-py/blob/v7.1.0/redis/connection.py#L852
| await super()._connect() | ||
| return None |
Copilot
AI
Dec 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic here is incorrect. When self.is_connected is True, the connection is already established and the method should simply return without calling super()._connect(). The check should just return early:
if self.is_connected:
return None # already connectedCalling await super()._connect() when already connected could cause issues as it attempts to set self._reader and self._writer again.
| await super()._connect() | |
| return None | |
| return None # already connected |
Description of change
#3874
Pull Request check-list
Please make sure to review and check all of these items:
NOTE: these things are not required to open a PR and can be done
afterwards / while the PR is open.