Skip to content

Commit 6e97dd3

Browse files
fastapi-sqlalchemy-pg-catalog/app: dispose engine pool on lifespan shutdown
Add try/finally around the lifespan yield so engine.dispose() runs at shutdown. Releases pooled psycopg2 connections cleanly across repeated start/stop cycles (local repro loops, CI lane reruns), which otherwise leak half-open postgres connections. Refs Copilot review on #102. Signed-off-by: Akash Kumar <meakash7902@gmail.com>
1 parent 226009f commit 6e97dd3

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

  • fastapi-sqlalchemy-pg-catalog/app

fastapi-sqlalchemy-pg-catalog/app/main.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,14 @@ async def lifespan(_: FastAPI):
8282
# FastAPI shape for any startup that touches a sync DB driver.
8383
await asyncio.to_thread(Base.metadata.create_all, engine)
8484
log.info("startup: create_all complete")
85-
yield
85+
try:
86+
yield
87+
finally:
88+
# Release pooled connections on shutdown so repeated
89+
# start/stop cycles (local repro loops, CI lanes) don't leak
90+
# half-open connections to postgres.
91+
engine.dispose()
92+
log.info("shutdown: engine pool disposed")
8693

8794

8895
app = FastAPI(lifespan=lifespan)

0 commit comments

Comments
 (0)