Skip to content

Commit 0becae3

Browse files
[3.13] gh-117657: Fix QSBR race condition (GH-118843) (#118905)
`_Py_qsbr_unregister` is called when the PyThreadState is already detached, so the access to `tstate->qsbr` isn't safe without locking the shared mutex. Grab the `struct _qsbr_shared` from the interpreter instead. (cherry picked from commit 33d2019) Co-authored-by: Alex Turner <[email protected]>
1 parent 0874a40 commit 0becae3

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

Include/internal/pycore_qsbr.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ _Py_qsbr_register(struct _PyThreadStateImpl *tstate,
140140

141141
// Disassociates a PyThreadState from the QSBR state and frees the QSBR state.
142142
extern void
143-
_Py_qsbr_unregister(struct _PyThreadStateImpl *tstate);
143+
_Py_qsbr_unregister(PyThreadState *tstate);
144144

145145
extern void
146146
_Py_qsbr_fini(PyInterpreterState *interp);

Python/pystate.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ tstate_delete_common(PyThreadState *tstate)
17941794
HEAD_UNLOCK(runtime);
17951795

17961796
#ifdef Py_GIL_DISABLED
1797-
_Py_qsbr_unregister((_PyThreadStateImpl *)tstate);
1797+
_Py_qsbr_unregister(tstate);
17981798
#endif
17991799

18001800
// XXX Unbind in PyThreadState_Clear(), or earlier

Python/qsbr.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -231,20 +231,21 @@ _Py_qsbr_register(_PyThreadStateImpl *tstate, PyInterpreterState *interp,
231231
}
232232

233233
void
234-
_Py_qsbr_unregister(_PyThreadStateImpl *tstate)
234+
_Py_qsbr_unregister(PyThreadState *tstate)
235235
{
236-
struct _qsbr_shared *shared = tstate->qsbr->shared;
236+
struct _qsbr_shared *shared = &tstate->interp->qsbr;
237+
struct _PyThreadStateImpl *tstate_imp = (_PyThreadStateImpl*) tstate;
237238

238239
PyMutex_Lock(&shared->mutex);
239240
// NOTE: we must load (or reload) the thread state's qbsr inside the mutex
240241
// because the array may have been resized (changing tstate->qsbr) while
241242
// we waited to acquire the mutex.
242-
struct _qsbr_thread_state *qsbr = tstate->qsbr;
243+
struct _qsbr_thread_state *qsbr = tstate_imp->qsbr;
243244

244245
assert(qsbr->seq == 0 && "thread state must be detached");
245-
assert(qsbr->allocated && qsbr->tstate == (PyThreadState *)tstate);
246+
assert(qsbr->allocated && qsbr->tstate == tstate);
246247

247-
tstate->qsbr = NULL;
248+
tstate_imp->qsbr = NULL;
248249
qsbr->tstate = NULL;
249250
qsbr->allocated = false;
250251
qsbr->freelist_next = shared->freelist;

Tools/tsan/suppressions_free_threading.txt

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ race_top:_PyParkingLot_Park
3838
race_top:_PyType_HasFeature
3939
race_top:assign_version_tag
4040
race_top:gc_restore_tid
41-
race_top:initialize_new_array
4241
race_top:insertdict
4342
race_top:lookup_tp_dict
4443
race_top:mi_heap_visit_pages

0 commit comments

Comments
 (0)