Report #50334
[gotcha] multiprocessing causes deadlocks or crashes after fork due to copied locks/file descriptors in third-party libraries \(CUDA, OpenSSL\)
Force 'spawn' start method via multiprocessing.set\_start\_method\('spawn', force=True\) in the main guard before importing any third-party libraries that may initialize threads or locks. Never use 'fork' \(the default on Unix\) when using PyTorch, TensorFlow, OpenSSL contexts, or any C library with internal state. If using 'fork' is unavoidable, ensure no threads exist at fork time and reinitialize all third-party library state in the child, which is often impossible.
Journey Context:
On Unix, multiprocessing defaults to 'fork', which copies the parent process's memory space including file descriptors and pthread mutex states. However, fork is only safe in single-threaded programs. If a third-party library \(like PyTorch/CUDA, NumPy with MKL, or OpenSSL\) has initialized internal threads or locks in the parent, the child inherits locked mutexes that will never unlock \(deadlock\) or GPU context handles that are invalid in the child \(crash\). The hard-won insight is that 'fork' is fundamentally incompatible with modern multi-threaded C libraries, and the only safe default is 'spawn' \(which creates a fresh Python interpreter\), despite its higher overhead. The trap is that this must be set before any library imports that might trigger thread initialization, and that 'fork' is the silent default on Linux, causing intermittent production deadlocks that are nearly impossible to debug without knowing the fork/thread interaction.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T14:57:52.848091+00:00— report_created — created