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: Sun, 04 Oct 2026 11:38:43 +0000
Message-ID: <gh-psycopg-psycopg-1427-c5979536928@github.com> (raw)
In-Reply-To: <gh-psycopg-psycopg-1427@github.com>
References: <gh-psycopg-psycopg-1427@github.com>

> So maybe it's the GC of a Django wrapper that calls `close()` on the psycopg server cursor?

From the stacktrace given by faulthandler in my initial message I think we can trust the 2 first frames: close is called by:

```python
def cursor_iter(cursor, sentinel, col_count, itersize):
    """
    Yield blocks of rows from a cursor and ensure the cursor is closed when
    done.
    """
    try:
        for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
            yield rows if col_count is None else [r[:col_count] for r in rows]
    finally:
        cursor.close()
```

in `django/db/models/sql/compiler.py`.

The next frame though I don’t think we can trust this, I bet the garbage collector mess with frames when it’s started, because it points to:

```python
        return self._tx.load_rows(0, res.ntuples, self._make_row)
```

line 183 of `psycopg/_server_cursor_base.py` in `_fetch_gen` and I don’t see how this line can directly call `cursor_iter`, so that should be where the GC triggered, if I understand correctly.

So the GC somehow triggered the `finally:` of this `cursor_iter`, **without** leaving a trace in the call stack (I bet GC does "things" that can make this happen), the only path I can imagine leading to that is: some `__del__` somewhere has a kind of "purge the generator before dying", calling a last `next()` on that `cursor_iter`, triggering the `finally:` clause.

Remembering that it does **not** happen when psycopg-binary is not loaded, and maybe the use of psycopg-binary kinds of mess with the stacktrace too (I don’t know I haven't read psycopg-binary code yet).



view thread (10+ messages)  latest in thread

Message-ID: <gh-psycopg-psycopg-1427-c5979536928@github.com>
Permalink:  ../gh-psycopg-psycopg-1427-c5979536928@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