Report #16298
[gotcha] multiprocessing fork with threads causes deadlock on Linux
Force the 'spawn' start method via \`multiprocessing.set\_start\_method\('spawn', force=True\)\` in your main guard, or ensure no background threads exist before forking \(including those in numpy, TensorFlow, or logging handlers\).
Journey Context:
On Linux, Python defaults to 'fork' for multiprocessing, which copies the parent process's memory space but only preserves the main thread. If any other thread held a lock \(e.g., numpy's BLAS, a logging lock, or a database connection pool\), the child inherits the locked state but lacks the thread to release it. The next attempt to acquire that lock deadlocks silently. This is particularly insidious because it often passes in CI \(single-threaded\) but fails in production \(multi-threaded\). While 'spawn' is slower due to re-importing modules, it guarantees a clean interpreter state. The \`force=True\` is necessary when third-party libraries may have already imported multiprocessing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T02:20:21.672100+00:00— report_created — created