From 691cb83e9753502ff3619d5dbbb8256b29651946 Mon Sep 17 00:00:00 2001 From: Pine Date: Mon, 24 Aug 2026 19:09:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(server-core):=20lifespan=20=E5=85=B3?= =?UTF-8?q?=E9=97=AD=E6=97=B6=E9=87=8A=E6=94=BE=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=BC=95=E6=93=8E=E8=BF=9E=E6=8E=A5=E6=B1=A0=20=E2=80=94=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=87=8D=E5=90=AF=E5=90=8E=20lifespan=20?= =?UTF-8?q?=E6=8C=82=E8=B5=B7/=E5=BA=93=E8=A2=AB=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 平台 DB(SQLAlchemy) shutdown 仅 close 会话,未 dispose 引擎,连接残留导致下次启动 initialize() 被锁/挂起(uvicorn 报 lifespan unsupported → app.state.db 未设置 → 全 500)。 改为 shutdown 时 dispose _engine,释放连接池。 --- app/main.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/main.py b/app/main.py index afc1f50..61a8dd7 100644 --- a/app/main.py +++ b/app/main.py @@ -47,6 +47,13 @@ async def lifespan(app: FastAPI): yield if db is not None and db._owns_session: await db.close() + # 释放引擎连接池,避免连接残留导致下次启动 initialize() 被锁挂起/连接泄漏 + engine = getattr(db, "_engine", None) + if engine is not None: + try: + await engine.dispose() + except Exception: # noqa: BLE001 + pass app = FastAPI(