#34389 closed Bug (needsinfo)

Django not closing database connections on ASGI application after an upgrade to 4.1

Reported by: vsobotka Owned by: nobody
Component: Uncategorized Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We just upgraded Django from version 2.2.12 to version 4.1.7. We are using a basic ASGI app config

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Landia.settings")
django.setup()
application = get_default_application()

We are on Heroku, with Heroku Postgres version 12.14, and we use psycopg2-binary version 2.9.5. We also use Gunicorn 20.1.0, and Uvicorn 0.20.0.

Database is connected

DATABASES['default'] = dj_database_url.config(conn_max_age=0)

Since we deployed to production, we have started to experience a database connection buildup, quickly using up the whole pool of connection allowed by Postgres. A few minutes after the deploy we only get exception about no available connections.

We tried upgrading Django to 4.2b1, and Psycopg 3, but the issue still persisted. Now we tried downgrading Django to 3.1.8, and the connections are at their usual level, not being used up at all, with everything else but Django on a different version. This leads me to believe Django is not closing connections, but it could be due to an issue with our setup.

I would greatly appreciate any help regarding this issue, as this is a production blocker for us, at least for 4.1.

Change History (1)

comment:1 by Mariusz Felisiak, 21 months ago

Resolution: needsinfo
Status: newclosed

Thanks for the report. ​Django has a routine to clean up old connections that is tied into the request-response life-cycle, so idle connections should be closed. However, I don't think you've explained the issue in enough detail to confirm a bug in Django. This can be an issue in psycopg2, uvicorn, or in custom middlewares (see #31905) it's hard to say without a sample project.

Please reopen the ticket if you can debug your issue and provide details about why and where Django is at fault, or if you can provide a sample project with reproducible scenario.

See also #33497 and #31134 for potential duplicates.

Note: See TracTickets for help on using tickets.
Back to Top