From: dvarrazzo (@dvarrazzo) <noreply+dvarrazzo@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 15:28:41 +0000
Message-ID: <gh-psycopg-psycopg-1427-c5981603519@github.com> (raw)
In-Reply-To: <gh-psycopg-psycopg-1427@github.com>
References: <gh-psycopg-psycopg-1427@github.com>
Yes, maybe the generator produced by this `cursor_iter()` is GC'd before completing, so `Cursor.close()` is called in its `__del__`.
I don't know exactly what to do our side: this code is not under our control but we can't blame it, it's pretty legit code. We should probably make the Django people aware of the issue and figure out if there's a different way to organise this code. I am not familiar with "the second `iter()` form" used here.
A defense that would work here would be adding a timeout on the lock acquisition on `close()`, but the need of a timeout is specific to only the GC invocation, which I don't see how to detect (because this is not our `__del__`), it wouldn't necessarily be appropriate for a `close()` in the normal execution realm, and it is several layers away (the lock is taken by the query executed by the server cursor close). I mean, any Psycopg path might end up being called by someone's destructor and end up in a similar deadlocking.
The effect of the C extension being loaded or not I think is undetermined: the execution of some C code only happens entirely inside the lock, whereas the lock is acquired in Python. `wait_c()` doesn't take a lock itself I seem to remember.