From 32367d783d0fbd1bf975dee0df804927212857df Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Thu, 9 Nov 2023 21:32:01 +0100 Subject: [PATCH] This ensures that the Redis connection is always properly closed. --- app/main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 5034333..c532175 100644 --- a/app/main.py +++ b/app/main.py @@ -18,9 +18,11 @@ logger = AppLogger().get_logger() async def lifespan(app: FastAPI): # Load the redis connection app.state.redis = await get_redis() - yield - # close redis connection and release the resources - app.state.redis.close() + try: + yield + finally: + # close redis connection and release the resources + app.state.redis.close() app = FastAPI(title="Stuff And Nonsense API", version="0.6", lifespan=lifespan)