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)