diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py index ac61314262..5a2dffdd1d 100644 --- a/redis/asyncio/cluster.py +++ b/redis/asyncio/cluster.py @@ -356,11 +356,13 @@ def __init__( ) self._initialize = True - self._lock = asyncio.Lock() + self._lock: Optional[asyncio.Lock] = None async def initialize(self) -> "RedisCluster": """Get all nodes from startup nodes & creates connections if not initialized.""" if self._initialize: + if not self._lock: + self._lock = asyncio.Lock() async with self._lock: if self._initialize: try: @@ -378,6 +380,8 @@ async def initialize(self) -> "RedisCluster": async def close(self) -> None: """Close all connections & client if initialized.""" if not self._initialize: + if not self._lock: + self._lock = asyncio.Lock() async with self._lock: if not self._initialize: self._initialize = True