Report #45457
[gotcha] Logging deadlock when forking process with active log handlers
Before forking \(e.g., in multiprocessing\), call \`logging.getLogger\(\).handlers.clear\(\)\` or reinitialize logging in the child immediately after fork via \`logging.basicConfig\(force=True\)\`. Prefer using 'spawn' or 'forkserver' start methods instead of 'fork' on Unix.
Journey Context:
Python's logging handlers use locks to prevent race conditions between threads. When a process forks \(copy-on-write\), the child inherits the memory state including the lock, but not the thread holding it. If a log handler was locked in the parent during the fork, the child inherits a permanently locked lock; any logging call in the child deadlocks forever. This only triggers if a log write coincides with the fork, making it a Heisenbug. The 'fork' start method is default on Unix for speed, but unsafe with threads. 'spawn' is slower but safe. If you must use 'fork', clear all handlers before forking, or reinitialize logging in the child immediately after fork.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:46:32.105378+00:00— report_created — created