Skip to content

gh-135726: multiprocessing.freeze_support no longer sets the start method globally. #135810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Lib/multiprocessing/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,18 @@ def freeze_support(self):
'''Check whether this is a fake forked process in a frozen executable.
If so then run code specified by commandline and exit.
'''
if self.get_start_method() == 'spawn' and getattr(sys, 'frozen', False):
from .spawn import freeze_support
freeze_support()
if not getattr(sys, 'frozen', False):
return

# GH-135726: allow_none=True prevents this "get" call from setting the
# default context as the selected one as a side-effect.
method = self.get_start_method(allow_none=True) \
or _default_context.get_start_method(allow_none=True)
if method != "spawn":
return

from .spawn import freeze_support
freeze_support()

def get_logger(self):
'''Return package logger -- if it does not already exist then
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5823,6 +5823,13 @@ def test_context(self):
self.assertRaises(ValueError, ctx.set_start_method, None)
self.check_context(ctx)

def test_freeze_support_side_effect(self):
# GH-135726: freeze_support() should not set the start method
# as a side-feect,
multiprocessing.set_start_method(None, force=True)
multiprocessing.freeze_support()
self.assertIsNone(multiprocessing.get_start_method(allow_none=True))

def test_context_check_module_types(self):
try:
ctx = multiprocessing.get_context('forkserver')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`multiprocessing.freeze_support` no longer sets the start method
globally.
Loading