Skip to content

Commit e261550

Browse files
authored
Fix memory allocation deadlock in qsbr code under free-threading.
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
1 parent 2b6a137 commit e261550

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

‎Python/qsbr.c‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,29 @@ _Py_qsbr_reserve(PyInterpreterState *interp)
197197
// Try allocating from our internal freelist
198198
struct _qsbr_thread_state *qsbr = qsbr_allocate(shared);
199199

200-
// If there are no free entries, we pause all threads, grow the array,
201-
// and update the pointers in PyThreadState to entries in the new array.
202-
if (qsbr == NULL) {
200+
while (qsbr == NULL) {
201+
// Unlock before stopping the world to avoid deadlocks.
202+
// If we hold shared->mutex while waiting for the world to stop,
203+
// we might block a thread that needs to acquire shared->mutex to park.
204+
PyMutex_Unlock(&shared->mutex);
203205
_PyEval_StopTheWorld(interp);
206+
PyMutex_Lock(&shared->mutex);
207+
208+
// Try allocating again, as another thread might have grown the array
209+
// or freed an entry while we were waiting.
210+
qsbr = qsbr_allocate(shared);
211+
if (qsbr != NULL) {
212+
_PyEval_StartTheWorld(interp);
213+
break;
214+
}
215+
216+
// Still NULL, we must grow it
204217
if (grow_thread_array(shared) == 0) {
205218
qsbr = qsbr_allocate(shared);
219+
} else {
220+
// Failed to grow array (e.g. OOM). Break to avoid infinite loop.
221+
_PyEval_StartTheWorld(interp);
222+
break;
206223
}
207224
_PyEval_StartTheWorld(interp);
208225
}

0 commit comments

Comments
 (0)