Skip to content

Commit 54c6fcb

Browse files
authored
gh-124375: Avoid calling _PyMem_ProcessDelayed on other thread states (#124459)
This fixes a crash when running the PyO3 test suite on the free-threaded build. The `qsbr` field is initialized after the `PyThreadState` is added to the interpreter's linked list -- it might still be NULL. Instead, we "steal" the queue of to-be-freed memory blocks. This is always initialized (possibly empty) and protected by the stop the world pause.
1 parent e97910c commit 54c6fcb

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash in the free threading build when the GC runs concurrently with a new thread starting.

Python/gc_free_threading.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,18 +420,24 @@ merge_queued_objects(_PyThreadStateImpl *tstate, struct collection_state *state)
420420
static void
421421
process_delayed_frees(PyInterpreterState *interp)
422422
{
423-
// In STW status, we can observe the latest write sequence by
424-
// advancing the write sequence immediately.
423+
// While we are in a "stop the world" pause, we can observe the latest
424+
// write sequence by advancing the write sequence immediately.
425425
_Py_qsbr_advance(&interp->qsbr);
426426
_PyThreadStateImpl *current_tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
427427
_Py_qsbr_quiescent_state(current_tstate->qsbr);
428+
429+
// Merge the queues from other threads into our own queue so that we can
430+
// process all of the pending delayed free requests at once.
428431
HEAD_LOCK(&_PyRuntime);
429-
PyThreadState *tstate = interp->threads.head;
430-
while (tstate != NULL) {
431-
_PyMem_ProcessDelayed(tstate);
432-
tstate = (PyThreadState *)tstate->next;
432+
for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
433+
_PyThreadStateImpl *other = (_PyThreadStateImpl *)p;
434+
if (other != current_tstate) {
435+
llist_concat(&current_tstate->mem_free_queue, &other->mem_free_queue);
436+
}
433437
}
434438
HEAD_UNLOCK(&_PyRuntime);
439+
440+
_PyMem_ProcessDelayed((PyThreadState *)current_tstate);
435441
}
436442

437443
// Subtract an incoming reference from the computed "gc_refs" refcount.

0 commit comments

Comments
 (0)