From: JulienPalard (@JulienPalard) <noreply+JulienPalard@github.com>
To: psycopg/psycopg <noreply+psycopg-psycopg@github.com>
Subject: [psycopg/psycopg] issue #1427: Hang in futex, linked to garbage collection
Date: Sat, 26 Sep 2026 18:10:57 +0000
Message-ID: <gh-psycopg-psycopg-1427@github.com> (raw)
I'm running a Django under gunicorn (sync worker), psycopg 3.3.6, (with and without pooling, the traces on this issue are with no pooling).
Sometimes workers get stuck and killed by gunicorn:
```
Sep 26 17:48:36 gunicorn[3740958]: [2026-09-26 17:48:36 +0000] [3740958] [CRITICAL] WORKER TIMEOUT (pid:3740964)
Sep 26 17:48:36 gunicorn[3740964]: Exception ignored in: <generator object cursor_iter at 0x7fb60279f890>
Sep 26 17:48:36 gunicorn[3740964]: Traceback (most recent call last):
Sep 26 17:48:36 gunicorn[3740964]: File "/opt/venv/lib/python3.13/site-packages/django/db/models/sql/compiler.py", line 2115, in cursor_iter
Sep 26 17:48:36 gunicorn[3740964]: cursor.close()
Sep 26 17:48:36 gunicorn[3740964]: File "/opt/venv/lib/python3.13/site-packages/psycopg/_server_cursor.py", line 69, in close
Sep 26 17:48:36 gunicorn[3740964]: with self._conn.lock:
Sep 26 17:48:36 gunicorn[3740964]: File "/opt/venv/lib/python3.13/site-packages/gunicorn/workers/base.py", line 199, in handle_abort
Sep 26 17:48:36 gunicorn[3740964]: sys.exit(1)
Sep 26 17:48:36 gunicorn[3740964]: SystemExit: 1
```
My good old friend, `strace`, shows that the process gets stuck in a `futex`:
```
3745521 1790445242.184237 sendto(8, "Q\0\0\0CFETCH FORWARD 2000 FROM \"_django_curs_140419890026368_sync_10\"\0", 68, MSG_NOSIGNAL, NULL, 0) = 68
3745521 1790445242.184311 poll([{fd=8, events=POLLIN|POLLERR}], 1, 100) = 1 ([{fd=8, revents=POLLIN}])
3745521 1790445242.184599 recvfrom(8, "T\0\0\3<\0\34id\0\0\0@[...]\0\rFETCH 20\0Z\0\0\0\5T", 16384, 0, NULL, NULL) = 6055
3745521 1790445242.185241 futex(0x7ffce596d980, FUTEX_WAIT_BITSET_PRIVATE|FUTEX_CLOCK_REALTIME, 0, NULL, FUTEX_BITSET_MATCH_ANY <unfinished ...>
```
According to strace there's a clean 4 worker processes that have no threads (the first one is the "gunicorn manager", the 4 others are the sync workers):
```
strace: Process 3740958 attached with 2 threads
strace: Process 3740961 attached
strace: Process 3740962 attached
strace: Process 3740963 attached
strace: Process 3745521 attached
```
Used `faulthandler` to get a traceback, got:
```
Current thread 0x00007fb60daa1780 (most recent call first):
Garbage-collecting
File "/opt/[...]/site-packages/psycopg/_server_cursor.py", line 69 in close
File "/opt/[...]/site-packages/django/db/models/sql/compiler.py", line 2115 in cursor_iter
File "/opt/[...]/site-packages/psycopg/_server_cursor_base.py", line 183 in _fetch_gen
File "/opt/[...]/site-packages/psycopg/connection.py", line 495 in wait
File "/opt/[...]/site-packages/psycopg/_server_cursor.py", line 121 in fetchmany
File "/opt/[...]/site-packages/django/db/utils.py", line 98 in inner
File "/opt/[...]/site-packages/django/db/models/sql/compiler.py", line 2112 in <lambda>
File "/opt/[...]/site-packages/django/db/models/sql/compiler.py", line 2112 in cursor_iter
File "/opt/[...]/site-packages/django/db/models/query.py", line 125 in __iter__
File "/opt/[...]/site-packages/django/db/models/query.py", line 520 in _iterator
```
I'm kind of out of ideas to give more useful debugging infos here, as I have no reproducer, don’t hesitate if you have ideas that I can try.
I noticed that it does not happen on the same query every time, but it always seems to happen in a transaction, leaving a:
```
633992+00 | 2026-09-26 17:54:02.184291+00 | 2026-09-26 17:54:02.184516+00 | Client | ClientRead | idle in transaction | 241194 | 241194 | | FETCH FORWARD 2000 FROM "_django_curs_140419890026368_sync_10" | client backend
```
psql side. Or maybe that's just me noticing it only during transactions because it locks a table. But it does not systematically happen on a cursor like this time, it can happen on a SELECT.
Interestingly enough it happens **only** during a `PATCH /api/admin/products/` on [django-oscar](https://github.com/django-oscar/django-oscar/), but maybe it's just because a PATCH on a product on Django Oscar triggers hundreds of SELECT and hundreds of UPDATES, making them a magnet for this bug.