Django 3.x / uwsgi is leaking connections
— at       Version 9
    
    
    
      
      
      
        
Project upgraded from 2.x to 3.0.4 is leaking PostgreSQL database connections.
ATOMIC_REQUESTS=False, AUTOCOMMIT=True, CONN_MAX_AGE=0
UWSGi threads are enabled (--enable-threads)  
Python 3.8.1 and 3.7.3
After few hours all posgtress connections are used (too many connections error). 
pg_stat_activity is reporting idle connections with part of queries visible.
 

Facts:
- setting up CONN_MAX_AGE > 0 does not help
-  CONN_MAX_AGE = 30, starting runserverand making many calls withab -c 10 -n 100 http://127.0.0.1:8000/uses all available connections and ends with error 500 (due to no available connections)
- CONN_MAX_AGE = 30, starting runserver --nothreadingand making calls withab -c 100 -n 100 http://127.0.0.1:8000/works fine
- CONN_MAX_AGE=30 + runserver + ab scenario is reproducible on clean install. Called view must access database.
- CONN_MAX_AGE=0 + runserver + ab -> cannot reproduce.
- Django 2.x has similar issue for CONN_MAX_AGE=30, but can reuse connections (still there are several errors on high traffic, but app server can "recover" and handle bunch of incoming requests)
- Looks like Django 3.x cannot reuse connections
- uwsgi + threads + CONN_MAX_AGE=0 on localhost -> cannot reproduce
Runserver (with threads) test results:
ab -c 10 -n 200  Django 3.0.4:
Complete requests:      200
Failed requests:        108
ab -c 10 -n 1000 Django 2.2.11:
Complete requests:      1000
Failed requests:        37
There may be two separate cases:
- not reusing connection when CONN_MAX_AGE is greater than 0
- leaking (not closing?) connections on uwsgi (?) with enabled threads (?) and CONN_MAX_AGE set to 0
I'm not sure about the 2nd case.