Uh oh!
There was an error while loading. Please reload this page.
gh-148953: Fix memory allocation deadlock in qsbr code under free-threading. - #149100
gh-148953: Fix memory allocation deadlock in qsbr code under free-threading.#149100AnnaAr321 wants to merge 4 commits into
Conversation
Previously, a thread attempting to allocate a QSBR thread state entry would acquire a lock (`shared->mutex`). If no free entry was immediately available, this thread, while still holding the lock, would trigger a "Stop The World" event to resize the underlying array. This could lead to a deadlock if other threads needed to acquire `shared->mutex` to park themselves as part of the "Stop The World" process. See python#148953
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
nascheme
commented
Jul 3, 2026
This looks like a good change to me. You should add a news item (can use the blurb tool). One small suggestion: the comment says threads may need |
nascheme
commented
Aug 7, 2026
Thanks for digging into this, but I don't think this change is correct, and I don't think the original code has the problem it's meant to fix. The patch introduces a deadlock. When applied,
Neither wait has a timeout, so the whole interpreter hangs. _Py_LOCK_DONT_DETACH doesn't help; the handoff comes from the other side. Holding the mutex across the stop-the-world is not a deadlock. Every acquirer of shared->mutex uses plain PyMutex_Lock, i.e. _PY_LOCK_DETACH. A thread that blocks on it detaches and is immediately counted as stopped, so it can never stall the STW. The comment in the patch ("we might block a thread that needs to acquire shared->mutex to park") isn't accurate — no thread needs that mutex in order to park; park_detached_threads() transitions other threads with a plain CAS. The mutex also can't simply be re-acquired after Finally, the two stacks in the issue don't show a cycle. A thread blocked on Closing this. The underlying report is still open. (Minor, for future reference: Python/qsbr.c is core runtime, so the NEWS entry belongs in Note: this code review was assisted by Claude Opus. |
Previously, a thread attempting to allocate a QSBR thread state entry would acquire a lock (
shared->mutex). If no free entry was immediately available, this thread, while still holding the lock, would trigger a "Stop The World" event to resize the underlying array. This could lead to a deadlock if other threads needed to acquireshared->mutexto park themselves as part of the "Stop The World" process.