Report #95325
[gotcha] Infinite recursion or double execution when using multiprocessing with spawn on macOS/Windows
Strictly guard all top-level executable code with \`if \_\_name\_\_ == '\_\_main\_\_':\`; define all target callables at module level; avoid creating Process/Pool instances as module globals.
Journey Context:
The spawn start method \(default on Windows/macOS\) creates a clean interpreter that re-imports the main module to reconstruct the target function by name. Without the guard, the import executes all top-level code, including creating new Processes, leading to infinite recursion until process limit. Defining callables inside the guard or as lambdas makes them unpicklable \(AttributeError: Can't get attribute\). Fork avoids this but copies memory including threads, causing deadlocks. The guard ensures the child finds the function definition without triggering execution side effects.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T18:34:53.171639+00:00— report_created — created