Deadlock while creating a "multiprocessing.Queue()" with acquired lock
I noticed a deadlock on PyPy 7.3.7 (both Python 3.7 and Python 3.8) involving:
I tested it on Arch Linux x64 locally. I'm not sure if it happens on Windows without calling
set_start_method() (used here to select the
"spawn" method which is the default on Windows).
Here is a minimal reproducible example:
import multiprocessing import threading import os if __name__ == "__main__": multiprocessing.set_start_method("spawn") lock = threading.Lock() def acquire(): print("Acquiring lock") lock.acquire() print("Acquired lock") def release(): print("Releasing lock") lock.release() print("Relased lock") os.register_at_fork(before=acquire, after_in_parent=release, after_in_child=release) with lock: multiprocessing.SimpleQueue()
You should notice it blocks on the
"Acquiring lock" message, while it works fine using CPython.
For more context about the use case: acquiring and releasing all existing locks during a
fork avoid possible deadlocks due to child process inheriting a acquired lock. It looks creaing a
multiprocessing.SimpleQueue() causes registered handlers to be called while no process is spawned.