Skip to content

Commit 396535f

Browse files
committed
Handle cancelled redis connection checks.
1 parent f21169a commit 396535f

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

starlette_plus/redis.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,21 @@ async def ping(self) -> bool:
5252
return self._could_connect
5353

5454
async def _health_task(self) -> None:
55-
while True:
56-
previous = self.could_connect
57-
await self.ping()
58-
59-
if not previous and self.could_connect:
60-
logger.info("Redis connection has been (re)established: %s", self.url)
61-
62-
await asyncio.sleep(5)
55+
try:
56+
while True:
57+
previous = self.could_connect
58+
await self.ping()
59+
60+
if not previous and self.could_connect:
61+
logger.info("Redis connection has been (re)established: %s", self.url)
62+
63+
await asyncio.sleep(5)
64+
except asyncio.CancelledError:
65+
logger.warning(
66+
'Redis connection: "%s" health check was cancelled. Safe to ignore on shutdown. '
67+
"If this was not intentional, all middleware relying on this connection will now be in-memory.",
68+
self.url,
69+
)
70+
self._could_connect = False
71+
finally:
72+
await self.pool.aclose()

0 commit comments

Comments
 (0)