Make multiprocessing SemLock
thread-safe in the free-threaded build
#117435
Labels
SemLock
thread-safe in the free-threaded build
#117435
Feature or enhancement
The multiprocessing
SemLock
code maintains an internalcount
field. When operating as a recursive mutex,count
is the number of times the thread has acquired the mutex (i.e., 0, 1, .. N). When operating as a semaphore,count
is often0
or1
, but can be negative if theSemLock
is initialized withmaxvalue > 1
The modification of
count
is not thread-safe without the GIL within the process. Note that thecount
field is not shared across processes, unlike the underlyingsem_t
orHANDLE
.In particular, the modification of
count
after the semaphore is released is not thread-safe without the GIL:cpython/Modules/_multiprocessing/semaphore.c
Lines 444 to 448 in 9dae05e
I think this is the source of deadlocks when running
test_multiprocessing_forkserver.test_processes
with the GIL disabled.Linked PRs
SemLock
thread-safe in free-threaded build #117436The text was updated successfully, but these errors were encountered: