Skip to content

Commit 2c4b386

Browse files
committed
Fix KeyError in async cluster - initialize before execute multi key commands (#2439)
* Fix KeyError in async cluster * link to issue * typo
1 parent 3db5e5e commit 2c4b386

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

redis/commands/cluster.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,25 @@ async def _split_command_across_slots(self, command: str, *keys: KeyT) -> int:
316316
# Sum up the reply from each command
317317
return sum(await self._execute_pipeline_by_slot(command, slots_to_keys))
318318

319+
async def _execute_pipeline_by_slot(
320+
self, command: str, slots_to_args: Mapping[int, Iterable[EncodableT]]
321+
) -> List[Any]:
322+
if self._initialize:
323+
await self.initialize()
324+
read_from_replicas = self.read_from_replicas and command in READ_COMMANDS
325+
pipe = self.pipeline()
326+
[
327+
pipe.execute_command(
328+
command,
329+
*slot_args,
330+
target_nodes=[
331+
self.nodes_manager.get_node_from_slot(slot, read_from_replicas)
332+
],
333+
)
334+
for slot, slot_args in slots_to_args.items()
335+
]
336+
return await pipe.execute()
337+
319338

320339
class ClusterManagementCommands(ManagementCommands):
321340
"""

tests/test_asyncio/test_cluster.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,15 @@ async def test_unlink(self, r: RedisCluster) -> None:
777777
await asyncio.sleep(0.1)
778778
assert await r.unlink(*d.keys()) == 0
779779

780+
async def test_initialize_before_execute_multi_key_command(
781+
self, request: FixtureRequest
782+
) -> None:
783+
# Test for issue https://github.com/redis/redis-py/issues/2437
784+
url = request.config.getoption("--redis-url")
785+
r = RedisCluster.from_url(url)
786+
assert 0 == await r.exists("a", "b", "c")
787+
await r.close()
788+
780789
@skip_if_redis_enterprise()
781790
async def test_cluster_myid(self, r: RedisCluster) -> None:
782791
node = r.get_random_node()

0 commit comments

Comments
 (0)