Hi everyone,
We’re investigating PostgreSQL connection exhaustion in a Django application running under WSGI with Gunicorn’s gevent workers.
Most database sessions are idle when the limit is reached. Restarting web provides temporary relief. Older sessions also disappear over time, so we have not established a permanent leak.
Our stack is Django 5.1, Gunicorn 23, gevent, and psycopg2. The relevant settings are:
"CONN_MAX_AGE": 60,
"CONN_HEALTH_CHECKS": True,
We understand that the age is measured in seconds and checked at request boundaries, rather than enforced by a background timer.
We ran a local experiment using the real application and authenticated read requests:
| Configuration | Requests | Connections created | Retained after requests |
|---|---|---|---|
| One gevent worker, age 60, separate HTTP connections | 30 | 30 | 7 |
| One gevent worker, age zero, separate HTTP connections | 30 | 30 | 0 |
| One gthread worker with two threads, age 60 | 30 | 2 | 2 reusable |
All requests returned HTTP 200. Instrumentation recorded response-close checks and request-finished signals for every request.
The seven retained gevent connections survived their owner greenlets and another 69 seconds. Explicit cyclic garbage collection in the isolated diagnostic worker released them.
A separate gevent control using one HTTP keepalive connection reused one database connection across 30 requests.
Our interpretation is that short requests leave healthy connections open before their deadlines. Later requests in different greenlet contexts do not perform age cleanup on those older wrappers.
We found Gunicorn issue #996, which describes similar behavior. We would appreciate guidance on three questions:
- Is this interpretation accurate for this worker model?
- Without a compatible pool, is web-only
CONN_MAX_AGE=0the appropriate baseline for gevent? - For a mostly synchronous Django application, what would you test when comparing bounded gthread workers with gevent plus pooling?
We’re preparing matched staging tests covering connection creation, retained sessions, latency, transactions, and worker replacement. The small experiment demonstrates behavior, not production capacity.
Thanks for any guidance.