#35455: psycopg3 warns about connections not being closed
-------------------------------------+-------------------------------------
Reporter: HTErik | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 5.0
layer (models, ORM) |
Severity: Normal | Keywords: psycopg
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
When running
* a Django 5.0.6 application
* psycopg 3.1.19
* with db `"ENGINE": "django.db.backends.postgresql"`
* with Python warnings as errors enabled
This occasionally prints following error from psycopg:
{{{
ResourceWarning: connection <psycopg.Connection [IDLE] (REDACTED) at
0x710d29abe120> was deleted while still open. Please use 'with' or
'.close()' to close the connection
}}}
This warning comes from the `__del__` function in
`psycopg.connection.BaseConnection`.
I have no idea how to reproduce this.
I tried patching psycopg to print a stack trace whenever a connection is
opened and closed. This tells me that the connection that was deleted
before being closed, was opened through the following code path:
{{{
File "blah.py", line 193, in __heartbeat
MyModel.objects.bulk_update(items_to_run, ["last_update"])
File "python3.12/site-packages/django/db/models/manager.py", line 87, in
manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "python3.12/site-packages/django/db/models/query.py", line 922, in
bulk_update
with transaction.atomic(using=self.db, savepoint=False):
File "python3.12/site-packages/django/db/transaction.py", line 198, in
__enter__
if not connection.get_autocommit():
File "python3.12/site-packages/django/db/backends/base/base.py", line
450, in get_autocommit
self.ensure_connection()
File "python3.12/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "python3.12/site-packages/django/db/backends/base/base.py", line
275, in ensure_connection
self.connect()
File "python3.12/site-packages/django/utils/asyncio.py", line 26, in
inner
return func(*args, **kwargs)
File "python3.12/site-packages/django/db/backends/base/base.py", line
256, in connect
self.connection = self.get_new_connection(conn_params)
}}}
--
Ticket URL: <
https://code.djangoproject.com/ticket/35455>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.