Skip to content

Data race reading _thread.RLock recursion count in repr() under free-threading #154928

Description

@Naserume

Bug report

Bug description:

This is a follow-up to #153292 (data race in repr() of _thread.RLock), which was fixed by making rlock_repr read the lock's owner with an atomic load. The fix covered the owner (self->lock.thread) field, but rlock_repr still reads self->lock.level with a plain (non-atomic) load to compute the recursion count:

staticPyObject*
rlock_repr(PyObject*op)
{
rlockobject*self=rlockobject_CAST(op);
PyThread_ident_towner=FT_ATOMIC_LOAD_ULLONG_RELAXED(self->lock.thread);
intlocked=rlock_locked_impl(self);
size_tcount;
if (locked) {
count=self->lock.level+1;
}
else {
count=0;
}
returnPyUnicode_FromFormat(
"<%s %s object owner=%"PY_FORMAT_THREAD_IDENT_T" count=%zu at %p>",
locked ? "locked" : "unlocked",
Py_TYPE(self)->tp_name, owner,
count, self);
}

self->lock.level is written by acquire / release / _acquire_restore, e.g.:

_thread_RLock__acquire_restore_impl(rlockobject*self, PyObject*state)
/*[clinic end generated code: output=beb8f2713a35e775 input=c8f2094fde059447]*/
{
PyThread_ident_towner;
Py_ssize_tcount;
if (!PyArg_Parse(state, "(n"Py_PARSE_THREAD_IDENT_T"):_acquire_restore",
&count, &owner))
returnNULL;
_PyRecursiveMutex_Lock(&self->lock);
_Py_atomic_store_ullong_relaxed(&self->lock.thread, owner);
self->lock.level= (size_t)count-1;

So on a free-threaded build, repr(rlock) concurrent with acquire() or release() is still a data race, now on the level field rather than the owner field the earlier fix addressed.

Reproducer:

import_threadfromthreadingimportThread, Barriershared_rlock=_thread.RLock()
state= (5, _thread.get_ident())
defchain1_thread():
for_inrange(20000):
try:
shared_rlock._acquire_restore(state)
exceptException:
passdefchain2_thread():
for_inrange(20000):
try:
repr(shared_rlock)
exceptException:
passN_C1=2N_C2=4barrier=Barrier(N_C1+N_C2)
def_c1():
barrier.wait()
chain1_thread()
def_c2():
barrier.wait()
chain2_thread()
threads= [Thread(target=_c1) for_inrange(N_C1)]
threads+= [Thread(target=_c2) for_inrange(N_C2)]
fortinthreads: t.start()
fortinthreads: t.join()

TSAN Report :

==================
WARNING: ThreadSanitizer: data race (pid=655652)
Read of size 8 at 0x7fffb6610540 by thread T6:
#0 rlock_repr /cpython/./Modules/_threadmodule.c:1295:28 #1 PyObject_Repr /cpython/Objects/object.c:784:11 #2 builtin_repr /cpython/Python/bltinmodule.c:2677:12 #3 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2712:35 Previous write of size 8 at 0x7fffb6610540 by thread T1:
#0 _thread_RLock__acquire_restore_impl /cpython/./Modules/_threadmodule.c:1210:22 #1 _thread_RLock__acquire_restore /cpython/./Modules/clinic/_threadmodule.c.h:537:20 #2 method_vectorcall_O /cpython/Objects/descrobject.c:476:24 #3 _PyObject_VectorcallTstate /cpython/./Include/internal/pycore_call.h:144:11 #4 PyObject_Vectorcall /cpython/Objects/call.c:327:12 #5 _Py_VectorCallInstrumentation_StackRefSteal /cpython/Python/ceval.c:768:11 #6 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:1906:35
SUMMARY: ThreadSanitizer: data race /cpython/./Modules/_threadmodule.c:1295:28 in rlock_repr
==================

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions