Report #12048
[gotcha] Deadlock or memory corruption when using os.fork\(\) in a multithreaded process
Never use os.fork\(\) \(or multiprocessing with 'fork' start method\) if any thread besides the main thread is running; explicitly set multiprocessing.set\_start\_method\('spawn'\) or 'forkserver' at program start, or ensure all background threads are joined before forking.
Journey Context:
When os.fork\(\) is called, only the calling thread survives in the child process. If other threads existed in the parent, they held locks \(e.g., in malloc, stdio, or Python's internal import machinery\) that remain locked forever in the child, causing immediate deadlock or heap corruption. This is insidious with third-party libraries \(e.g., monitoring, logging\) that spawn background threads. The fix is to use 'spawn' \(the default on Windows/macOS since 3.8\) which starts a fresh interpreter, avoiding the issue entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T14:54:18.919725+00:00— report_created — created