Blocking workflow runs pin a DB connection for the whole run; a burst hangs requests and empties the pool for up to an hour
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. - Create a Workflow app: Start → HTTP Request (GET an endpoint that answers after about 2 seconds) → End. Publish it and create an API key.
- Send 60 requests at the same time to
POST /v1/workflows/runwith"response_mode": "blocking". - Afterwards, look at the connections that are still open:
select state, count(*), max(now() - state_change)
from pg_stat_activity where datname = 'dify' group by 1;
✔️ Expected Behavior
Requests beyond the pool capacity wait and then complete, or fail quickly. No request hangs, and no connection stays checked out after the burst.
❌ Actual Behavior
| 60 concurrent blocking runs | Succeeded | HTTP 500 | No response within the client timeout | Connections still idle in transaction afterwards |
|---|---|---|---|---|
| 1.19.0 | 8 | 35 | 17 (client timeout 120 s) | 17 |
| 1.19.0 with #46244 applied | 0 | 20 | 40 (client timeout 300 s) | 40, which is the whole pool |
| 1.19.0 with #46244 and the fix below | 60 (3.9 s) | 0 | 0 | 0 |
With #46244 applied (it removes an earlier wait in admission, see #46241) the burst reaches this code path in full: 40 requests hang, the pool stays empty, and every later request to the API fails with a QueuePool timeout. A single streaming request sent six minutes later still returned 500. With 30 concurrent requests, below the pool capacity, everything works (2.7 s).
There are two defects behind this.
1. A blocking run keeps the caller's connection for the whole run. AppGenerateService._dispatch_generate resolves the workflow through the handler session (_get_workflow(..., session=session)), which leaves that session inside a transaction. It then calls WorkflowAppGenerator.generate(..., streaming=False), which runs the whole workflow before returning. The generator does call db.session.close() with the comment "release database connection, because the following new thread operations may take a long time", but the session that holds the connection is the handler session, which the generator never receives. AdvancedChatAppGenerator gets the session and closes it; the workflow generator does not. So each blocking request pins one connection while the run opens its own sessions, and once in-flight requests reach the pool capacity none of them can proceed.
2. A failure while preparing the run is never reported to the waiting request. In WorkflowAppGenerator._generate_worker, loading the workflow and building the runner happen before the try block that publishes errors to the queue:
sqlalchemy.exc.TimeoutError: QueuePool limit of size 30 overflow 10 reached, connection timed out, timeout 30.00
File "/app/api/core/app/apps/workflow/app_generator.py", line 652, in _generate_worker
The worker thread dies, and the request keeps waiting on the queue, still holding its connection, until the execution watchdog fires. APP_MAX_EXECUTION_TIME defaults to 3600 seconds. That is why the hung requests never come back and why their connections stay idle in transaction. The same happens for any other preparation failure, for example ValueError("Workflow not found").
The second defect is not limited to blocking mode: _generate_worker is also the worker for streaming runs executed by Celery, where a preparation failure leaves the task slot occupied and the client stream open with pings only.
I will open a pull request that releases the caller session before a blocking workflow run and publishes preparation failures like run failures.