Skip to content

Commit

Permalink
async_cluster: fix simultaneous initialize (#2231)
Browse files Browse the repository at this point in the history
- close startup_nodes too during client.close(), in case they are different
  • Loading branch information
utkarshgupta137 authored Jun 19, 2022
1 parent 2ee61f0 commit 3370298
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions redis/asyncio/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions tests/test_asyncio/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3370298

Please sign in to comment.