Skip to content

Commit ad7e977

Browse files
committed
Fixed issue with Sentinel killing healthcheck thread before execution
1 parent 73cb068 commit ad7e977

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

redis/connection.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,13 +1307,7 @@ def __init__(
13071307
# release the lock.
13081308
self._fork_lock = threading.Lock()
13091309
self.reset()
1310-
1311-
# Run scheduled healthcheck to avoid stale invalidations in idle connections.
1312-
if self.cache is not None and self._scheduler is not None:
1313-
self._hc_cancel_event = threading.Event()
1314-
self._hc_thread = self._scheduler.run_with_interval(
1315-
self._perform_health_check, 2, self._hc_cancel_event
1316-
)
1310+
self.run_scheduled_healthcheck()
13171311

13181312
def __repr__(self) -> (str, str):
13191313
return (
@@ -1513,6 +1507,14 @@ def set_retry(self, retry: "Retry") -> None:
15131507
for conn in self._in_use_connections:
15141508
conn.retry = retry
15151509

1510+
def run_scheduled_healthcheck(self) -> None:
1511+
# Run scheduled healthcheck to avoid stale invalidations in idle connections.
1512+
if self.cache is not None and self._scheduler is not None:
1513+
self._hc_cancel_event = threading.Event()
1514+
self._hc_thread = self._scheduler.run_with_interval(
1515+
self._perform_health_check, 2, self._hc_cancel_event
1516+
)
1517+
15161518
def _perform_health_check(self, done: threading.Event) -> None:
15171519
self._checkpid()
15181520
with self._lock:

redis/sentinel.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def get_master_address(self):
115115
connection_pool = self.connection_pool_ref()
116116
if connection_pool is not None:
117117
connection_pool.disconnect(inuse_connections=False)
118+
connection_pool.run_scheduled_healthcheck()
118119
return master_address
119120

120121
def rotate_slaves(self):

tests/test_cache.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import threading
21
import time
32

43
import pytest
@@ -38,7 +37,7 @@ def r(request):
3837

3938
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
4039
@pytest.mark.onlynoncluster
41-
# @skip_if_resp_version(2)
40+
@skip_if_resp_version(2)
4241
class TestCache:
4342
@pytest.mark.parametrize(
4443
"r",

0 commit comments

Comments
 (0)