Report #12976
[gotcha] Subprocess preexec\_fn causes deadlock when process has multiple threads
Never use preexec\_fn in multi-threaded programs; use start\_new\_session=True instead of os.setsid\(\) in preexec\_fn, or refactor to avoid preexec\_fn entirely.
Journey Context:
People use preexec\_fn to call os.setsid\(\) or ulimit. But fork\(\) only copies the calling thread; if other threads held locks \(e.g., in malloc or Python's memory allocator\), the child deadlocks on the next allocation. The fix is using start\_new\_session=True \(3.2\+\) which safely does setsid in the child post-fork without Python-level code.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T17:24:06.141576+00:00— report_created — created