From 86510e8dbcee899813766a494df0ad01b8ad5a62 Mon Sep 17 00:00:00 2001 From: Manuel Holtgrewe Date: Wed, 3 Jan 2024 14:52:16 +0100 Subject: [PATCH] fix: more robust handling of async sessions (#360) (#361) --- backend/app/api/deps.py | 7 +++---- backend/app/main.py | 2 ++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/backend/app/api/deps.py b/backend/app/api/deps.py index b19e72b9..bf9fb57c 100644 --- a/backend/app/api/deps.py +++ b/backend/app/api/deps.py @@ -1,13 +1,12 @@ from typing import AsyncIterator from app.core import auth -from app.db.session import SessionLocal, engine +from app.db.session import SessionLocal current_active_user = auth.fastapi_users.current_user(active=True) current_active_superuser = auth.fastapi_users.current_user(active=True, superuser=True) async def get_db() -> AsyncIterator[SessionLocal]: # type: ignore[valid-type] - db = SessionLocal() - yield db - await db.close() + async with SessionLocal() as db: + yield db diff --git a/backend/app/main.py b/backend/app/main.py index f0347440..73a1f414 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -15,6 +15,7 @@ from app.api.internal.endpoints.remote import httpx_client_wrapper from app.core.config import settings from app.db.init_db import create_superuser +from app.db.session import engine if settings.DEBUG: logging.basicConfig(level=logging.DEBUG) @@ -40,6 +41,7 @@ async def lifespan(app: FastAPI): httpx_client_wrapper.start() yield await httpx_client_wrapper.stop() + await engine.dispose() app = FastAPI(