Session connections not properly released when using FastAPI Depends, causing connection pool exhaustion #1417
Replies: 2 comments 2 replies
-
I'm facing a similar issue, but when using SQLModel with Celery workers. When I process a lot of tasks really quickly, the connection pool gets exhausted |
Beta Was this translation helpful? Give feedback.
-
This issue comes from FastAPI, not SQLModel. If I'm right, it will only happen when you specified the response model. Try removing response model from your endpoint (or, return UPD: in the initial code example there is even more obvious problem - blocking database IO is used inside |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Bug Description
When using SQLModel Session with FastAPI's Depends() dependency injection, database connections are not properly released back to the connection pool after request completion. This leads to connection pool exhaustion and application blocking when the pool limit is reached.
Environment
SQLModel version: 0.0.24
FastAPI version: 0.115.14
Database: SQLite
Python version: 3.12.5
Expected Behavior
Database connections should be automatically released back to the connection pool after each request completes, allowing the application to handle concurrent requests without exhausting the pool.
Actual Behavior
Connections remain checked out from the pool and are not released, causing:
Steps to Reproduce
If you change the endpoint to hit the
/user-session-begin
, notice that this issue does not happen.##Questions
Operating System
Linux
Operating System Details
PRETTY_NAME="Ubuntu 24.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.2 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
UBUNTU_CODENAME=noble
SQLModel Version
0.024
Python Version
3.12.5
Additional Context
sample k6 script to load test
Beta Was this translation helpful? Give feedback.
All reactions