Report #65907
[gotcha] Logging deadlock when using os.fork\(\) in multi-threaded Python programs
Configure all logging handlers exclusively inside the child process after forking, or switch multiprocessing to use the 'spawn' start method instead of 'fork' \(the default on Unix\).
Journey Context:
The logging module uses internal locks \(threading.RLock\) to protect shared state like file streams and formatter state. When os.fork\(\) is called, the child process inherits the exact memory state of the parent, including lock states. If any thread in the parent process held a logging lock at the moment of fork, that lock remains permanently locked in the child. When the child process attempts to log, it tries to acquire the same lock and deadlocks immediately. This is a race condition that appears random and only affects the 'fork' start method \(default on Unix\). The 'spawn' method avoids this by starting a fresh interpreter without duplicated lock states, making it the correct choice for multi-threaded applications.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T17:06:21.961833+00:00— report_created — created