From bbf4cebd9080222d0e8b73393231fdf7ffcf8b54 Mon Sep 17 00:00:00 2001 From: gavindiaz Date: Sun, 25 Oct 2026 03:28:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E9=9B=85=E5=81=9C=E6=9C=BA?= =?UTF-8?q?=E6=97=B6=E4=B8=BB=E5=8A=A8=20dispose=20=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=B1=A0=EF=BC=8C=E6=B6=88=E9=99=A4=20SQLite=20=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E8=BF=9E=E6=8E=A5=E8=A2=AB=20GC=20=E4=B8=A2=E5=BC=83?= =?UTF-8?q?=E7=9A=84=20SAWarning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ctrl+C 停服时 aiosqlite 后台线程上的连接若未归还池会被 GC 直接丢弃, 表现为 shutdown 时的 non-checked-in connection SAWarning。在 lifespan 关闭 阶段调用 engine.dispose() 主动清理(数据不受影响,纯停机噪音修复)。 Co-Authored-By: Claude Code --- backend/app/main.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/app/main.py b/backend/app/main.py index f4c2bbc..7f6276e 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -8,7 +8,7 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from app.core.config import settings -from app.core.database import init_db, Base +from app.core.database import init_db, Base, engine from app.core.security import verify_token from app.api.books import router as books_router from app.api.chat import router as chat_router @@ -25,7 +25,12 @@ from app.models.mcp_server import McpServer @asynccontextmanager async def lifespan(app: FastAPI): await init_db() - yield + try: + yield + finally: + # 优雅停机:主动销毁连接池,避免 SQLite 后台线程的连接被 GC 丢弃 + # (表现为 shutdown 时 SAWarning:non-checked-in connection ... dropped) + await engine.dispose() app = FastAPI(title="BookMate", version="0.1.0", lifespan=lifespan)