Report #59325
[gotcha] Multiprocessing hangs or deadlocks on Linux when threads exist before Process spawn
Explicitly force the start method to 'spawn' or 'forkserver' via multiprocessing.set\_start\_method\('spawn', force=True\) in the main guard; never use the default 'fork' on Linux if any threads have been started.
Journey Context:
On Linux, multiprocessing defaults to 'fork', which copies only the calling thread into the child process. If other threads existed in the parent, they vanish in the child but may have held locks \(e.g., in glibc malloc, the GIL itself, or logging handlers\). The child inherits these locks in a locked state; when it tries to acquire them, it deadlocks immediately. This is a POSIX limitation, not a Python bug. Python 3.8 changed macOS default to 'spawn' because of this issue, but Linux retains 'fork' for performance. The fix is using 'spawn' \(slower, starts fresh interpreter\) or 'forkserver' \(pre-forks a clean server process\). Critical constraint: set\_start\_method must be called before any Process objects are created, ideally inside \`if \_\_name\_\_ == '\_\_main\_\_':\`. The symptom is a hang on process start, often when using PyTorch, TensorFlow, or any library with background threads.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T06:04:15.633465+00:00— report_created — created