gh-154928: Fix data race on the recursion count of threading.RLock - #155381
gh-154928: Fix data race on the recursion count of threading.RLock#155381deadlovelll wants to merge 1 commit into
Conversation
|
I want to give a little disclaimer about my solution, maybe it would answer the possible questions during the review. At the first attempt i tried to change type of _PyRecursiveMutex's where it shoots into the knee: import _thread
r = _thread.RLock()
r._acquire_restore((0, _thread.get_ident()))
r.release()When we execute this code with Assertion failed: (m->level == 0), function _PyRecursiveMutex_TryUnlock,
file lock.c, line 466.Because in Then in int
_PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
{
PyThread_ident_t thread = PyThread_get_thread_ident_ex();
if (!recursive_mutex_is_owned_by(m, thread)) {
return -1;
}
if (m->level > 0) {
FT_ATOMIC_STORE_SIZE_RELAXED(m->level, m->level - 1);
return 0;
}
assert(m->level == 0);
_Py_atomic_store_ullong_relaxed(&m->thread, 0);
PyMutex_Unlock(&m->mutex);
return 0;
}So in this fix I preferred to be conservative and keep the current semantics, despite the bigger diff. My first version - deadlovelll@c2f316e |
Fix data race on the recursion count of threading.RLock
For more details see gh-154928