python runtime_error ai_generated true

sqlalchemy.exc.TimeoutError: QueuePool 大小 5、溢出 10 已达上限,连接超时 30.00 秒。

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

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

其他格式: JSON · Markdown 中文 · English
80%修复率
88%置信度
0证据数
2024-09-17首次发现

版本兼容性

版本状态引入弃用备注
0.100+ active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

    Delays exhaustion; underlying leak persists.

  2. 85% 失败

    Detects dead connections, does not free leaked ones.