Skip to content

Commit 52738a0

Browse files
gpsheadclaude
andcommitted
gh-140814: Fix freeze_support() setting start method as side effect
freeze_support() called get_start_method() without allow_none=True, which locked in the default start method context. This caused a subsequent set_start_method() call to raise "context has already been set". Use allow_none=True and accept None as a matching value, since spawn.freeze_support() independently detects spawned child processes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cfeede8 commit 52738a0

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Lib/multiprocessing/context.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ def freeze_support(self):
145145
'''Check whether this is a fake forked process in a frozen executable.
146146
If so then run code specified by commandline and exit.
147147
'''
148-
if self.get_start_method() == 'spawn' and getattr(sys, 'frozen', False):
148+
# gh-140814: allow_none=True avoids locking in the default start
149+
# method, which would cause a later set_start_method() to fail.
150+
# None is safe to pass through: spawn.freeze_support()
151+
# independently detects whether this process is a spawned
152+
# child, so the start method check here is only an optimization.
153+
if (getattr(sys, 'frozen', False)
154+
and self.get_start_method(allow_none=True) in ('spawn', None)):
149155
from .spawn import freeze_support
150156
freeze_support()
151157

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:func:`multiprocessing.freeze_support` no longer sets the default start method
2+
as a side effect, which previously caused a subsequent
3+
:func:`multiprocessing.set_start_method` call to raise ``RuntimeError``.

0 commit comments

Comments
 (0)