Skip to content

Commit d566cf6

Browse files
committed
Update BaseContext.freeze_support instead of override it
1 parent 3d383d7 commit d566cf6

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

Lib/multiprocessing/context.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,18 @@ 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):
149-
from .spawn import freeze_support
150-
freeze_support()
148+
if not getattr(sys, 'frozen', False):
149+
return
150+
151+
# GH-135726: allow_none=True prevents this "get" call from setting the
152+
# default context as the selected one as a side-effect.
153+
method = self.get_start_method(allow_none=True) \
154+
or _default_context.get_start_method(allow_none=True)
155+
if method != "spawn":
156+
return
157+
158+
from .spawn import freeze_support
159+
freeze_support()
151160

152161
def get_logger(self):
153162
'''Return package logger -- if it does not already exist then
@@ -266,17 +275,6 @@ def get_all_start_methods(self):
266275
)
267276
return start_method_names
268277

269-
def freeze_support(self):
270-
'''Check whether this is a fake forked process in a frozen executable.
271-
If so then run code specified by commandline and exit.
272-
'''
273-
start_method = self.get_start_method(allow_none=True)
274-
if start_method is None:
275-
start_method = self._default_context.get_start_method()
276-
if start_method == 'spawn' and getattr(sys, 'frozen', False):
277-
from .spawn import freeze_support
278-
freeze_support()
279-
280278

281279
#
282280
# Context types for fixed start method

Lib/test/_test_multiprocessing.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5823,6 +5823,13 @@ def test_context(self):
58235823
self.assertRaises(ValueError, ctx.set_start_method, None)
58245824
self.check_context(ctx)
58255825

5826+
def test_freeze_support_side_effect(self):
5827+
# GH-135726: freeze_support() should not set the start method
5828+
# as a side-feect,
5829+
multiprocessing.set_start_method(None, force=True)
5830+
multiprocessing.freeze_support()
5831+
self.assertIsNone(multiprocessing.get_start_method(allow_none=True))
5832+
58265833
def test_context_check_module_types(self):
58275834
try:
58285835
ctx = multiprocessing.get_context('forkserver')

0 commit comments

Comments
 (0)