Skip to content

Commit

Permalink
pythongh-110481: Fix biased reference counting queue initialization.
Browse files Browse the repository at this point in the history
The biased reference counting queue must be initialized from the bound
(active) thread because it uses `_Py_ThreadId()` as the key in a hash
table.
  • Loading branch information
colesbury committed Mar 26, 2024
1 parent 507896d commit e697b37
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Python/brc.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ _Py_brc_merge_refcounts(PyThreadState *tstate)
struct _brc_thread_state *brc = &((_PyThreadStateImpl *)tstate)->brc;
struct _brc_bucket *bucket = get_bucket(tstate->interp, brc->tid);

assert(brc->tid == _Py_ThreadId());

// Append all objects into a local stack. We don't want to hold the lock
// while calling destructors.
PyMutex_Lock(&bucket->mutex);
Expand Down
10 changes: 6 additions & 4 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ bind_tstate(PyThreadState *tstate)
tstate->native_thread_id = PyThread_get_thread_native_id();
#endif

#ifdef Py_GIL_DISABLED
// Initialize biased reference counting inter-thread queue. Note that this
// needs to be initialized from the active thread.
_Py_brc_init_thread(tstate);
#endif

// mimalloc state needs to be initialized from the active thread.
tstate_mimalloc_bind(tstate);

Expand Down Expand Up @@ -1412,10 +1418,6 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->what_event = -1;
tstate->previous_executor = NULL;

#ifdef Py_GIL_DISABLED
// Initialize biased reference counting inter-thread queue
_Py_brc_init_thread(tstate);
#endif
llist_init(&_tstate->mem_free_queue);

if (interp->stoptheworld.requested || _PyRuntime.stoptheworld.requested) {
Expand Down

0 comments on commit e697b37

Please sign in to comment.