diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py index 051d567d457928..00bb89a67f1392 100644 --- a/Lib/multiprocessing/context.py +++ b/Lib/multiprocessing/context.py @@ -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 diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index a1c30bcfd51747..50c747f15ff17b 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -5932,6 +5932,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') diff --git a/Misc/NEWS.d/next/Library/2025-06-22-15-42-10.gh-issue-135726._oO5IN.rst b/Misc/NEWS.d/next/Library/2025-06-22-15-42-10.gh-issue-135726._oO5IN.rst new file mode 100644 index 00000000000000..ef8070988683a2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-22-15-42-10.gh-issue-135726._oO5IN.rst @@ -0,0 +1,2 @@ +:func:`multiprocessing.freeze_support` no longer sets the start method +globally.