Report #10508
[gotcha] Deadlock when using multiprocessing with threads or locks on POSIX systems
Force the 'spawn' start method at program entry point: \`multiprocessing.set\_start\_method\('spawn', force=True\)\` \(Windows default\). Alternatively, use 'forkserver' \(Unix only\), or ensure absolutely no locks \(including import locks or logging locks\) are held during process creation. Never use the default 'fork' method in multi-threaded programs.
Journey Context:
POSIX fork\(\) duplicates the memory space but not the threads. If a thread held a lock \(threading.Lock, import lock, or even a logging handler lock\) during the fork, the child process inherits the locked state with no thread to release it, causing immediate deadlock. Python's multiprocessing defaults to 'fork' on Unix for speed, hiding this landmine. This manifests as hangs in production when mixing threads \(e.g., in a web server\) with ProcessPoolExecutor. 'Spawn' is slower \(re-imports modules\) but safe because it starts a fresh Python interpreter without duplicating the parent's memory/lock state.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:51:21.294334+00:00— report_created — created