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

Conversation

aisk
Copy link
Contributor

@aisk aisk commented Jun 22, 2025

Copy link
Member

@gpshead gpshead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you come up with a meaningful regression test for this?

Comment on lines 273 to 276
start_method = self.get_start_method(allow_none=True)
if start_method is None:
start_method = self._default_context.get_start_method()
if start_method == 'spawn' and getattr(sys, 'frozen', False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than adding this method to DefaultContext, which complicates the picture of which one is called when and why two are defined in the heirarchy, perhaps edit the one in BaseContext to something like this:

diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py
index 051d567d457..2d5c9a1d3dd 100644
--- a/Lib/multiprocessing/context.py
+++ b/Lib/multiprocessing/context.py
@@ -145,7 +145,11 @@ 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):
+        # GH-135726: allow_none=True prevents this "get" call from setting the
+        # default context as the selected one as a side-effect.
+        if (getattr(sys, 'frozen', False) and
+            isinstance(self.get_start_method(allow_none=True)
+                       or _default_context, SpawnContext)):
             from .spawn import freeze_support
             freeze_support()
 

(untested - I'm just trying to reason though the logic)

@aisk
Copy link
Contributor Author

aisk commented Jun 26, 2025

Thank you for the review, updated!

@marcelotduarte
Copy link

@aisk This second commit will not break the patches I already have in use in cx_Freeze, because I monkeypatched freeze_support. And it does not interfere with the start method in Python (since it is not frozen). On Windows, this should work as it was before the PR that caused the regression. On Linux and macOS, I will continue monkeypatching until the gh-76327 is applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants