pg.ddx.io  psycopg/psycopg GitHub issues and pull requests  
help / color / mirror / Atom feed
From: JulienPalard (@JulienPalard) <noreply+JulienPalard@github.com>
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Subject: Re: [psycopg/psycopg] issue #1427: Hang in futex, linked to garbage collection
Date: Sat, 03 Oct 2026 08:02:39 +0000
Message-ID: <gh-psycopg-psycopg-1427-c5966997844@github.com> (raw)
In-Reply-To: <gh-psycopg-psycopg-1427@github.com>
References: <gh-psycopg-psycopg-1427@github.com>

> If a GC triggers `ServerCursor.close()` then it means that the cursor is not managed with a context manager, right?

Used tracemalloc, you’re right it’s not managed by a context manager, it’s created from django.db.backends.postgresql.base:

```py
    @async_unsafe
    def create_cursor(self, name=None):
        if name:
            if is_psycopg3 and (
                self.settings_dict["OPTIONS"].get("server_side_binding") is not True
            ):
                # psycopg >= 3 forces the usage of server-side bindings for
                # named cursors so a specialized class that implements
                # server-side cursors while performing client-side bindings
                # must be used if `server_side_binding` is disabled (default).
===>>           cursor = ServerSideCursor(
                    self.connection,
                    name=name,
                    scrollable=False,
                    withhold=self.connection.autocommit,
                )
            else:
                # In autocommit mode, the cursor will be used outside of a
                # transaction, hence use a holdable cursor.
                cursor = self.connection.cursor(
                    name, scrollable=False, withhold=self.connection.autocommit
                )
        else:
            cursor = self.connection.cursor()

        if is_psycopg3:
            # Register the cursor timezone only if the connection disagrees, to
            # avoid copying the adapter map.
            tzloader = self.connection.adapters.get_loader(TIMESTAMPTZ_OID, Format.TEXT)
            if self.timezone != tzloader.timezone:
                register_tzloader(self.timezone, cursor)
        else:
            cursor.tzinfo_factory = self.tzinfo_factory if settings.USE_TZ else None
        return cursor
```

The allocation traceback of the cursor is:

```text
.venv/lib/python3.13/site-packages/oscar/apps/catalogue/product_attributes.py:259
.venv/lib/python3.13/site-packages/oscar/apps/catalogue/product_attributes.py:225
.venv/lib/python3.13/site-packages/oscar/apps/catalogue/product_attributes.py:26
.venv/lib/python3.13/site-packages/django/db/models/query.py:520
.venv/lib/python3.13/site-packages/django/db/models/query.py:93
.venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py:1570
.venv/lib/python3.13/site-packages/django/utils/asyncio.py:26
.venv/lib/python3.13/site-packages/django/db/backends/postgresql/base.py:462
.venv/lib/python3.13/site-packages/django/db/backends/base/base.py:297
.venv/lib/python3.13/site-packages/django/utils/asyncio.py:26
.venv/lib/python3.13/site-packages/django/db/backends/postgresql/base.py:416
```

with a Django 5.1.15. I've not dug how Django manage the lifetime of its cursors yet.

view thread (10+ messages)  latest in thread

Message-ID: <gh-psycopg-psycopg-1427-c5966997844@github.com>
Permalink:  ../gh-psycopg-psycopg-1427-c5966997844@github.com/

 · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox