diff --git a/Lib/multiprocessing/context.py b/Lib/multiprocessing/context.py index 051d567d457928..a7e319575832d0 100644 --- a/Lib/multiprocessing/context.py +++ b/Lib/multiprocessing/context.py @@ -236,9 +236,7 @@ def __init__(self, context): def get_context(self, method=None): if method is None: - if self._actual_context is None: - self._actual_context = self._default_context - return self._actual_context + return self._actual_context or self._default_context else: return super().get_context(method) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index a1259ff1d63d18..9b5ec8cf174bee 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -5811,6 +5811,12 @@ def check_context(self, ctx): p.join() self.assertEqual(child_method, ctx.get_start_method()) + def test_default_context(self): + # Get_context should not have side effect, see gh-109070. + multiprocessing.set_start_method(None, force=True) + multiprocessing.get_context() + self.assertIsNone(multiprocessing.context._default_context._actual_context) + def test_context(self): for method in ('fork', 'spawn', 'forkserver'): try: diff --git a/Misc/NEWS.d/next/Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst b/Misc/NEWS.d/next/Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst new file mode 100644 index 00000000000000..e2f45736a438ce --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-18-23-59-40.gh-issue-109070.2XNJ7X.rst @@ -0,0 +1 @@ +:func:`multiprocessing.get_context` will not set the start method globally.