﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35455	psycopg3 warns about connections not being closed	HTErik	nobody	"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)
}}}
"	Bug	closed	Database layer (models, ORM)	5.0	Normal	invalid	psycopg		Unreviewed	0	0	0	0	0	0
