https://github.com/python/cpython/commit/e20863f2239950ca5e316a3ffab9f60abb1b5f48
commit: e20863f2239950ca5e316a3ffab9f60abb1b5f48
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: gpshead <[email protected]>
date: 2025-12-24T03:03:47Z
summary:

[3.13] gh-109263: Start process from spawn context in multiprocessing no longer 
have side effect (GH-135813) (#143116)

gh-109263: Start process from spawn context in multiprocessing no longer have 
side effect (GH-135813)
(cherry picked from commit c2202a7e661d40b1837cc0109cdb9ab40ec4e486)

Co-authored-by: AN Long <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-06-22-18-57-19.gh-issue-109263.f92V95.rst
M Lib/multiprocessing/spawn.py
M Lib/test/_test_multiprocessing.py

diff --git a/Lib/multiprocessing/spawn.py b/Lib/multiprocessing/spawn.py
index daac1ecc34b55e..d43864c939cb63 100644
--- a/Lib/multiprocessing/spawn.py
+++ b/Lib/multiprocessing/spawn.py
@@ -184,7 +184,7 @@ def get_preparation_data(name):
         sys_argv=sys.argv,
         orig_dir=process.ORIGINAL_DIR,
         dir=os.getcwd(),
-        start_method=get_start_method(),
+        start_method=get_start_method(allow_none=True),
         )
 
     # Figure out whether to initialise main in the subprocess as a module
diff --git a/Lib/test/_test_multiprocessing.py 
b/Lib/test/_test_multiprocessing.py
index 7b46e4297c1503..08555f1693a7ba 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -5689,6 +5689,25 @@ def test_context(self):
             self.assertRaises(ValueError, ctx.set_start_method, None)
             self.check_context(ctx)
 
+    @staticmethod
+    def _dummy_func():
+        pass
+
+    def test_spawn_dont_set_context(self):
+        # Run a process with spawn or forkserver context may change
+        # the global start method, see gh-109263.
+        for method in ('fork', 'spawn', 'forkserver'):
+            multiprocessing.set_start_method(None, force=True)
+
+            try:
+                ctx = multiprocessing.get_context(method)
+            except ValueError:
+                continue
+            process = ctx.Process(target=self._dummy_func)
+            process.start()
+            process.join()
+            
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-18-57-19.gh-issue-109263.f92V95.rst 
b/Misc/NEWS.d/next/Library/2025-06-22-18-57-19.gh-issue-109263.f92V95.rst
new file mode 100644
index 00000000000000..6b96b5b9b2a0de
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-06-22-18-57-19.gh-issue-109263.f92V95.rst
@@ -0,0 +1,2 @@
+Starting a process from spawn context in :mod:`multiprocessing` no longer
+sets the start method globally.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to