1919 DEFAULT_HEALTH_CHECK_INTERVAL ,
2020 DEFAULT_LOCK_TTL ,
2121 DEFAULT_SOCKET_TIMEOUT ,
22- SHUTDOWN_TIMEOUT_S ,
2322)
2423
2524_logger = logging .getLogger (__name__ )
@@ -34,6 +33,7 @@ class RedisClientSDK:
3433
3534 _client : aioredis .Redis = field (init = False )
3635 _health_check_task : Task | None = None
36+ _health_check_task_started_event : asyncio .Event | None = None
3737 _is_healthy : bool = False
3838
3939 @property
@@ -56,10 +56,13 @@ def __post_init__(self) -> None:
5656 client_name = self .client_name ,
5757 )
5858 # NOTE: connection is done here already
59- self ._is_healthy = True
59+ self ._is_healthy = False
60+ self ._health_check_task_started_event = asyncio .Event ()
6061
6162 @periodic (interval = self .health_check_interval )
6263 async def _periodic_check_health () -> None :
64+ assert self ._health_check_task_started_event # nosec
65+ self ._health_check_task_started_event .set ()
6366 self ._is_healthy = await self .ping ()
6467
6568 self ._health_check_task = asyncio .create_task (
@@ -75,9 +78,11 @@ async def _periodic_check_health() -> None:
7578
7679 async def shutdown (self ) -> None :
7780 if self ._health_check_task :
78- await cancel_wait_task (
79- self ._health_check_task , max_delay = SHUTDOWN_TIMEOUT_S
80- )
81+ assert self ._health_check_task_started_event # nosec
82+ await (
83+ self ._health_check_task_started_event .wait ()
84+ ) # NOTE: wait for the health check task to start
85+ await cancel_wait_task (self ._health_check_task )
8186
8287 await self ._client .aclose (close_connection_pool = True )
8388
0 commit comments