diff --git a/redis/cluster.py b/redis/cluster.py index 9726ab93b1..1e8bbf8583 100644 --- a/redis/cluster.py +++ b/redis/cluster.py @@ -1057,10 +1057,10 @@ def execute_command(self, *args, **kwargs): # Return the processed result return self._process_result(args[0], res, **kwargs) except Exception as e: - if is_default_node: - # Replace the default cluster node - self.replace_default_node() if retry_attempts > 0 and type(e) in self.__class__.ERRORS_ALLOW_RETRY: + if is_default_node: + # Replace the default cluster node + self.replace_default_node() # The nodes and slots cache were reinitialized. # Try again with the new cluster setup. retry_attempts -= 1 diff --git a/tests/test_asyncio/test_cluster.py b/tests/test_asyncio/test_cluster.py index d2808fcb28..cec2dc09a4 100644 --- a/tests/test_asyncio/test_cluster.py +++ b/tests/test_asyncio/test_cluster.py @@ -800,7 +800,6 @@ async def test_default_node_is_replaced_after_exception(self, r): # CLUSTER NODES command is being executed on the default node nodes = await r.cluster_nodes() assert "myself" in nodes.get(curr_default_node.name).get("flags") - # Mock connection error for the default node mock_node_resp_exc(curr_default_node, ConnectionError("error")) # Test that the command succeed from a different node diff --git a/tests/test_cluster.py b/tests/test_cluster.py index 4cc2f5f103..43aeb9e045 100644 --- a/tests/test_cluster.py +++ b/tests/test_cluster.py @@ -813,8 +813,6 @@ def raise_connection_error(): nodes = r.cluster_nodes() assert "myself" not in nodes.get(curr_default_node.name).get("flags") assert r.get_default_node() != curr_default_node - # Rollback to the old default node - r.replace_default_node(curr_default_node) @pytest.mark.onlycluster