Deadlock in test_multiprocessing_pool_circular_import
in free-threaded build
#118332
Labels
test_multiprocessing_pool_circular_import
in free-threaded build
#118332
Bug report
I've observed an occasional deadlock involving the warnings mutex in
test_multiprocessing_pool_circular_import
. That test runs the following program, which involves both threads and multiprocessing.cpython/Lib/test/test_importlib/partial/pool_in_threads.py
Lines 1 to 27 in 5a90de0
Backtraces
There is a deadlock between thread 2 and thread 5:
Thread 2 (stopping the world, waiting for warning lock via critical section resume)
Thread 5 (holds warning lock via handoff, waiting for stop-the-world)
Explanation
There are two subtle things leading to the deadlock:
PyEvent_WaitTimed
call, which temporarily detaches and then re-attaches the thread state. In the process of re-attaching, the PyThreadState resumes the top most critical section, which in this case is trying to re-acquire the warnings mutex. (That's a bit weird!)Possible fixes:
PyEvent_WaitTimed()
in the stop-the-world. Basically, add a variant ofPyEvent_WaitTimed
that accepts_Py_LOCK_DONT_DETACH
so we never suspend/resume the critical section in the thread performing the stop the world.stop_the_world
(and remove it instart_the_world
). The thread can still detach, but when it re-attaches it doesn't try to resume any critical sections (until it's done performing the stop-the-world).Both (1) and (2) seem reasonable to me. I'm not sure which is better.
Linked PRs
Footnotes
PyMutex
implements stochastic fairness. Basically, after waiting for a while, locks are handed off directly to the next waiter in line without unlocking the mutex so that eventually everyone gets a turn. ↩The text was updated successfully, but these errors were encountered: