Report #56901
[gotcha] Multiprocessing deadlock when using threads before fork on Linux
Explicitly set multiprocessing start method to 'spawn' at the very beginning of your program: if \_\_name\_\_ == '\_\_main\_\_': multiprocessing.set\_start\_method\('spawn'\). Never rely on the default 'fork' on Linux if your program uses any threading \(including in third-party libraries like NumPy or logging\) before forking.
Journey Context:
Linux defaults to 'fork' for performance \(copy-on-write memory\), which copies the process space but not threads. If any thread held a lock \(e.g., in glibc's malloc, Python's logging, or NumPy's BLAS\) during the fork, the child inherits the locked state but no thread to unlock it, causing immediate deadlock on the first allocation or lock acquisition. 'spawn' is slower \(reimports modules\) but safe because it starts a fresh Python interpreter. The tradeoff is startup time vs safety.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T01:59:50.713420+00:00— report_created — created