[Django] #34389: Django not closing database connections on ASGI application after an upgrade to 4.1

8 views
Skip to first unread message

Django

unread,
Mar 6, 2023, 4:14:22 AM3/6/23
to django-...@googlegroups.com
#34389: Django not closing database connections on ASGI application after an
upgrade to 4.1
-----------------------------------------+------------------------
Reporter: vsobotka | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 4.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34389>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 6, 2023, 4:37:21 AM3/6/23
to django-...@googlegroups.com
#34389: Django not closing database connections on ASGI application after an
upgrade to 4.1
-------------------------------+--------------------------------------
Reporter: vsobotka | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 4.1
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => needsinfo


Comment:

Thanks for the report. ​Django has a
[https://github.com/django/django/blob/b05dfc289429dc4de990617da543af681d6638c7/django/db/__init__.py#L53-L61
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/34389#comment:1>

Reply all
Reply to author
Forward
0 new messages