[BUG] - Requests hold a Postgres transaction open while queued, exhausting the 15-connection pool under concurrent load
First Check
- This is not a feature request.
- I added a very descriptive title to this issue (title field is above this).
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the Mealie documentation, with the integrated search.
- I already read the docs and didn't find an answer.
- This issue can be replicated on the demo site (https://demo.mealie.io/).
What is the issue you are experiencing?
Under concurrent load, Mealie permanently wedges: it keeps accepting TCP connections on
port 9000 but never returns an HTTP response, and the container goes unhealthy. Only a
restart recovers it.
The usual symptom reported for this is sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached. I think the existing reports have identified the symptom
rather than the cause, so here is what I found from the database side.
Mealie holds a Postgres transaction open for the entire request lifetime, including while the request is merely queued and doing no database work at all.
Observed on a healthy instance while 60 concurrent requests were in flight:
state | wait_event_type | wait_event | count | longest_txn
---------------------+-----------------+------------+-------+----------------
idle in transaction | Client | ClientRead | 7 | 00:00:00.72725
active | | | 3 | 00:00:00.03369At 80 concurrent requests this reaches exactly 15, the pool ceiling, with only 1 connection doing any work:
sample|active|2
sample|idle in transaction|10
sample|active|1
sample|idle in transaction|15 <-- pool fully consumedSo all 15 pooled connections end up held by requests that are idle, waiting for an anyio
threadpool worker, while Postgres sits in ClientRead waiting for a command that will not
come until a worker frees up. Any new request then blocks 30s on pool checkout and fails.
This is survivable if the client backs off. It is not survivable with a client that retries without backoff. In my case that is the Home Assistant Mealie integration, which retried at ~112 requests/second and generated 13,450 errors in a single hour. Once the pool is saturated the queue never drains and Mealie never recovers on its own.
Two things make this hard to work around:
mealie/db/db_setup.py:38callscreate_enginewith nopool_sizeormax_overflow, so the pool is SQLAlchemy's default 5 + 10 = 15, and there is no environment variable to raise it:engine = sa.create_engine(db_url, echo=False, connect_args=connect_args, pool_pre_ping=True, future=True)- Because the sessions are
idle in transactionrather thanactive, Postgres'sstatement_timeoutdoes not clear them.
To be clear about what I could not reproduce: I initially assumed this was a leak on client disconnect. It is not. I fired 20 requests and aborted every one mid-query, and all connections returned to the pool cleanly. The problem is purely that transactions are held open across the queueing period, not that they are leaked.
Steps to Reproduce
- Run Mealie with
DB_ENGINE=postgres. - Fire enough concurrent requests at any read endpoint to exceed the 15-connection pool.
The timeline endpoint is a good candidate because it is slow (~1.5s here):
for i in $(seq 1 80); do curl -s -o /dev/null -H "Authorization: Bearer $TOKEN" \ "http://mealie:9000/api/recipes/timeline/events?page=1&perPage=500" & done - While that runs, query Postgres:
select state, wait_event_type, wait_event, count(*), max(now()-xact_start) from pg_stat_activity where datname='mealie' group by 1,2,3 order by 4 desc; - Observe
idle in transactionclimb to 15 whileactivestays at 1 to 3. - Keep the load sustained, as a non-backing-off client would, and Mealie stops responding entirely and does not recover.
Please provide relevant logs
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/20/3o7r)
File "/opt/mealie/lib/python3.12/site-packages/mealie/routes/households/controller_mealplan.py", line 97, in get_all
return self.repo.page_all(pagination=q)
File "/opt/mealie/lib/python3.12/site-packages/mealie/repos/repository_generic.py", line 341, in page_all
q, count, total_pages = self.add_pagination_to_query(q, pagination_result)
File "/opt/mealie/lib/python3.12/site-packages/mealie/repos/repository_generic.py", line 379, in add_pagination_to_query
count = self.session.scalar(count_query)
...
File "/opt/mealie/lib/python3.12/site-packages/sqlalchemy/pool/impl.py", line 167, in _do_get
raise exc.TimeoutError(Database state during the incident, all 15 from Mealie's own address:
state | wait_event_type | wait_event | count | longest
---------------------+-----------------+------------+-------+-----------------
idle in transaction | Client | ClientRead | 15 | 00:07:34.539424
active | | | 1 | 00:00:00Possible fixes
Suggestions in rough order of how much they would help:
- Do not hold the session open while the request is queued. Acquire the connection inside the threadpool worker, at the point the request actually starts doing database work, rather than during dependency resolution on the event loop.
- Make the pool configurable, for example
DB_POOL_SIZEandDB_MAX_OVERFLOW, so the ceiling can at least be matched to the threadpool size. Right now the 40-thread pool and the 15-connection pool are badly mismatched. - Consider
pool_use_lifo=Trueand an explicitpool_recycle.
Workaround for anyone hitting this, which makes the instance self-heal rather than needing a restart:
ALTER ROLE mealie SET idle_in_transaction_session_timeout = '60s';Mealie Version
v3.26.0 (image built 2026-09-14, revision 84b2677f76069a6fb2bac6e2c633c9dbe7379ce4)
Deployment
Docker (Linux)
Additional Deployment Details
Postgres backend on a separate host. LOG_LEVEL=DEBUG. Client driving the load is the
Home Assistant Mealie integration polling the mealplan endpoint.
Possibly related, all closed as stale rather than fixed: #4604 (also triggered by Home Assistant), #4793, #5810, #6393.
Source: mealie-recipes/mealie