Skip to content

Commit

Permalink
cluster: don't leak IndexError in NodesManager.initialize
Browse files Browse the repository at this point in the history
If the cluster has just been started, `CLUSTER SLOTS` returns an empty
array. `NodesManager.initialize` did not take this into account and took
the first element unconditionally causing `IndexError`.

This commit makes it raise `ValkeyClusterException` with a clear error
message instead.

Signed-off-by: Mikhail Koviazin <[email protected]>
  • Loading branch information
mkmkme committed Sep 4, 2024
1 parent c9ff993 commit 798569b
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion valkey/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,10 @@ def initialize(self):
self.nodes_cache = tmp_nodes_cache
self.slots_cache = tmp_slots
# Set the default node
self.default_node = self.get_nodes_by_server_type(PRIMARY)[0]
try:
self.default_node = self.get_nodes_by_server_type(PRIMARY)[0]
except IndexError:
raise ValkeyClusterException("No primary nodes found in the cluster")
if self._dynamic_startup_nodes:
# Populate the startup nodes with all discovered nodes
self.startup_nodes = tmp_nodes_cache
Expand Down

0 comments on commit 798569b

Please sign in to comment.