Chat, completion and agent-chat requests hold pooled DB connections across model resolution and for the whole model call
Self Checks
- [x] I have read the Contributing Guide and Language Policy.
- [x] This is only for bug report, if you would like to ask a question, please head to Discussions.
- [x] I have searched for existing issues search for existing issues, including closed ones.
- [x] I confirm that I am using English to submit this report, otherwise it will be closed.
- [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
- [x] Please do not modify this template :) and fill in all the required fields.
Dify version
1.19.0 (the affected code is unchanged on main at 4833948da)
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
- Start Dify 1.19.0 with Docker Compose, default database pool (
SQLALCHEMY_POOL_SIZE=30,SQLALCHEMY_MAX_OVERFLOW=10), andSERVER_WORKER_CONNECTIONS=1000so the API accepts more concurrent requests than the pool can serve. Apply #46244 so that requests get past Service API admission (#46241). - Create a Chat app (basic chatbot, memory on) on any model whose answer takes about 2 seconds, publish it and create an API key.
- Send 60 requests at the same time to
POST /v1/chat-messages, streaming or blocking, each with a differentuser. - While the answers are being generated, look at the connections:
select state, count(*) from pg_stat_activity where datname = 'dify' group by 1;
✔️ Expected Behavior
A chat request needs a database connection only for its short reads and writes. While the model is generating, its connections are back in the pool, so the number of chat requests in flight is not limited by the pool size.
❌ Actual Behavior
One API process (gevent), pool 30 + 10, a model that streams its answer over 2 seconds, N requests sent at once:
Chat app, POST /v1/chat-messages |
30 concurrent | 60 concurrent | Connections during generation (30 concurrent) |
|---|---|---|---|
| 1.19.0 with #46244, streaming | 30/30 in 2.6 s | 0/60, all HTTP 500 after 30 s | 30 idle in transaction |
| 1.19.0 with #46244, blocking | 30/30 in 2.5 s | 10/60, 50 × HTTP 500 | 30 idle in transaction |
Every failed request is a pool timeout:
sqlalchemy.exc.TimeoutError: QueuePool limit of size 30 overflow 10 reached, connection timed out, timeout 30.00
File "/app/api/controllers/service_api/app/completion.py", line 446, in post
File "/app/api/core/app/apps/chat/app_generator.py", line 183, in generate
File "/app/api/core/app/app_config/easy_ui_based_app/model_config/converter.py", line 25, in convert
File "/app/api/core/provider_manager.py", line 650, in get_configurations
There are two separate holds, measured one at a time:
1. The request needs two connections at once before generation starts. ChatAppGenerator.generate reads the conversation, the model config and the annotation settings through the handler session, which leaves that session inside a transaction. It then calls ModelConfigConverter.convert, and the provider manager behind it opens sessions of its own (and may call the plugin daemon). Once the number of requests at this point reaches the pool capacity, every one of them holds one connection and waits for a second one, and they all fail together after SQLALCHEMY_POOL_TIMEOUT. CompletionAppGenerator.generate and AgentChatAppGenerator.generate have the same sequence. MessageMoreLikeThisGenerator already avoids it by isolating the scoped session before the same call.
2. The worker keeps a connection for the whole model call. ChatAppRunner.run is written to run "without retaining session during model I/O" and closes its explicit session before streaming. But the conversation history is read through TokenBufferMemory.get_history_prompt_messages, which uses the thread's scoped session (db.session()) and leaves it inside the read transaction. Nothing closes that session until the worker thread finishes, so one connection per chat request stays idle in transaction while the model generates. PreparedHistory documents that "callers should first close the history session" before rendering; this caller does not.
Fixing only one of the two is not enough:
| 60 concurrent, streaming | Result | Connections during generation |
|---|---|---|
| Neither fixed | 0/60 | 40 idle in transaction (whole pool) |
| Only (1) fixed | 60/60 in 5.0 s, in two waves | 40 idle in transaction (whole pool) |
| Only (2) fixed | 0/60 | 40 idle in transaction (whole pool) |
| Both fixed | 60/60 in 2.8 s | all idle |
With only (1) fixed the pool size is still the ceiling for chat requests in flight: the 41st request waits until an earlier answer has finished, and a model that takes longer than the 30 s pool timeout turns that wait into errors.
AgentAppGenerator.generate calls ModelConfigConverter.convert at the same point with the handler session open; I have not measured that path.
I will open a pull request for both holds.