fix(server-core): lifespan 关闭时释放数据库引擎连接池 — 修复重启后 lifespan 挂起/库被锁

平台 DB(SQLAlchemy) shutdown 仅 close 会话,未 dispose 引擎,连接残留导致下次启动
initialize() 被锁/挂起(uvicorn 报 lifespan unsupported → app.state.db 未设置 → 全 500)。
改为 shutdown 时 dispose _engine,释放连接池。
This commit is contained in:
Pine
2026-08-24 19:09:35 +08:00
parent 88882553a9
commit 691cb83e97
+7
View File
@@ -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(