python runtime_error ai_generated true

sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00

ID: python/fastapi-db-session-not-closed

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-09-17First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100+ active — — —

Root Cause

Dependency yields a DB session but never closes it on exceptions, leaking connections until the pool is exhausted.

generic

中文

依赖项 yield 了数据库会话但异常时未关闭,连接泄漏直至连接池耗尽。

Workarounds

  1. 97% success
    def get_db():
        db = SessionLocal()
        try:
            yield db
        finally:
            db.close()
  2. 96% success
    def get_db():
        with SessionLocal() as db:
            yield db

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Delays exhaustion; underlying leak persists.

  2. 85% fail

    Detects dead connections, does not free leaked ones.