You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The multiprocessing SemLock code maintains an internal count 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 often 0 or 1, but can be negative if the SemLock is initialized with maxvalue > 1
The modification of count is not thread-safe without the GIL within the process. Note that the count field is not shared across processes, unlike the underlying sem_t or HANDLE.
In particular, the modification of count after the semaphore is released is not thread-safe without the GIL:
colesbury
changed the title
Make multiprocessing semaphore thread-safe in the free-threaded build
Make multiprocessing SemLock thread-safe in the free-threaded build
Apr 1, 2024
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: