Skip to content

[3.14] gh-134679: Fix assertion failure in QSBR (gh-134811) #134814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in the :term:`free threading` build's QSBR code that could occur
when changing an object's ``__dict__`` attribute.
22 changes: 11 additions & 11 deletions Objects/obmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,15 +1238,15 @@ work_queue_first(struct llist_node *head)
}

static void
process_queue(struct llist_node *head, struct _qsbr_thread_state *qsbr,
process_queue(struct llist_node *head, _PyThreadStateImpl *tstate,
bool keep_empty, delayed_dealloc_cb cb, void *state)
{
while (!llist_empty(head)) {
struct _mem_work_chunk *buf = work_queue_first(head);

if (buf->rd_idx < buf->wr_idx) {
struct _mem_work_item *item = &buf->array[buf->rd_idx];
if (!_Py_qsbr_poll(qsbr, item->qsbr_goal)) {
if (!_Py_qsbr_poll(tstate->qsbr, item->qsbr_goal)) {
return;
}

Expand All @@ -1270,19 +1270,19 @@ process_queue(struct llist_node *head, struct _qsbr_thread_state *qsbr,

static void
process_interp_queue(struct _Py_mem_interp_free_queue *queue,
struct _qsbr_thread_state *qsbr, delayed_dealloc_cb cb,
_PyThreadStateImpl *tstate, delayed_dealloc_cb cb,
void *state)
{
assert(PyMutex_IsLocked(&queue->mutex));
process_queue(&queue->head, qsbr, false, cb, state);
process_queue(&queue->head, tstate, false, cb, state);

int more_work = !llist_empty(&queue->head);
_Py_atomic_store_int_relaxed(&queue->has_work, more_work);
}

static void
maybe_process_interp_queue(struct _Py_mem_interp_free_queue *queue,
struct _qsbr_thread_state *qsbr, delayed_dealloc_cb cb,
_PyThreadStateImpl *tstate, delayed_dealloc_cb cb,
void *state)
{
if (!_Py_atomic_load_int_relaxed(&queue->has_work)) {
Expand All @@ -1291,7 +1291,7 @@ maybe_process_interp_queue(struct _Py_mem_interp_free_queue *queue,

// Try to acquire the lock, but don't block if it's already held.
if (_PyMutex_LockTimed(&queue->mutex, 0, 0) == PY_LOCK_ACQUIRED) {
process_interp_queue(queue, qsbr, cb, state);
process_interp_queue(queue, tstate, cb, state);
PyMutex_Unlock(&queue->mutex);
}
}
Expand All @@ -1303,10 +1303,10 @@ _PyMem_ProcessDelayed(PyThreadState *tstate)
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;

// Process thread-local work
process_queue(&tstate_impl->mem_free_queue, tstate_impl->qsbr, true, NULL, NULL);
process_queue(&tstate_impl->mem_free_queue, tstate_impl, true, NULL, NULL);

// Process shared interpreter work
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl->qsbr, NULL, NULL);
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl, NULL, NULL);
}

void
Expand All @@ -1316,10 +1316,10 @@ _PyMem_ProcessDelayedNoDealloc(PyThreadState *tstate, delayed_dealloc_cb cb, voi
_PyThreadStateImpl *tstate_impl = (_PyThreadStateImpl *)tstate;

// Process thread-local work
process_queue(&tstate_impl->mem_free_queue, tstate_impl->qsbr, true, cb, state);
process_queue(&tstate_impl->mem_free_queue, tstate_impl, true, cb, state);

// Process shared interpreter work
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl->qsbr, cb, state);
maybe_process_interp_queue(&interp->mem_free_queue, tstate_impl, cb, state);
}

void
Expand Down Expand Up @@ -1348,7 +1348,7 @@ _PyMem_AbandonDelayed(PyThreadState *tstate)

// Process the merged queue now (see gh-130794).
_PyThreadStateImpl *this_tstate = (_PyThreadStateImpl *)_PyThreadState_GET();
process_interp_queue(&interp->mem_free_queue, this_tstate->qsbr, NULL, NULL);
process_interp_queue(&interp->mem_free_queue, this_tstate, NULL, NULL);

PyMutex_Unlock(&interp->mem_free_queue.mutex);

Expand Down
Loading