From 33702983b8b0a55d29189babb631ea108ee8404f Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta Date: Sun, 19 Jun 2022 07:16:02 +0530 Subject: [PATCH] async_cluster: fix simultaneous initialize (#2231) - close startup_nodes too during client.close(), in case they are different --- redis/asyncio/cluster.py | 4 ++-- tests/test_asyncio/test_cluster.py | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py index 89789b7d75..a7bea3029a 100644 --- a/redis/asyncio/cluster.py +++ b/redis/asyncio/cluster.py @@ -323,14 +323,13 @@ async def initialize(self) -> "RedisCluster": if self._initialize: async with self._lock: if self._initialize: - self._initialize = False try: await self.nodes_manager.initialize() await self.commands_parser.initialize( self.nodes_manager.default_node ) + self._initialize = False except BaseException: - self._initialize = True await self.nodes_manager.close() await self.nodes_manager.close("startup_nodes") raise @@ -343,6 +342,7 @@ async def close(self) -> None: if not self._initialize: self._initialize = True await self.nodes_manager.close() + await self.nodes_manager.close("startup_nodes") async def __aenter__(self) -> "RedisCluster": return await self.initialize() diff --git a/tests/test_asyncio/test_cluster.py b/tests/test_asyncio/test_cluster.py index 0c676cb2d7..e5ec0267ea 100644 --- a/tests/test_asyncio/test_cluster.py +++ b/tests/test_asyncio/test_cluster.py @@ -680,13 +680,15 @@ async def test_not_require_full_coverage_cluster_down_error( else: raise e - async def test_can_run_concurrent_commands(self, r: RedisCluster) -> None: - assert await r.ping(target_nodes=RedisCluster.ALL_NODES) is True + async def test_can_run_concurrent_commands(self, request: FixtureRequest) -> None: + url = request.config.getoption("--redis-url") + rc = RedisCluster.from_url(url) assert all( await asyncio.gather( - *(r.ping(target_nodes=RedisCluster.ALL_NODES) for _ in range(100)) + *(rc.echo("i", target_nodes=RedisCluster.ALL_NODES) for i in range(100)) ) ) + await rc.close() @pytest.mark.onlycluster