Skip to content

Commit 64461f1

Browse files
miss-islingtonaisk
andauthored
[3.14] gh-109263: Start process from spawn context in multiprocessing no longer have side effect (GH-135813) (#143115)
gh-109263: Start process from spawn context in multiprocessing no longer have side effect (GH-135813) (cherry picked from commit c2202a7) Co-authored-by: AN Long <aisk@users.noreply.github.com>
1 parent e6b11c8 commit 64461f1

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/multiprocessing/spawn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def get_preparation_data(name):
184184
sys_argv=sys.argv,
185185
orig_dir=process.ORIGINAL_DIR,
186186
dir=os.getcwd(),
187-
start_method=get_start_method(),
187+
start_method=get_start_method(allow_none=True),
188188
)
189189

190190
# Figure out whether to initialise main in the subprocess as a module

Lib/test/_test_multiprocessing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5845,6 +5845,25 @@ def test_context(self):
58455845
self.assertRaises(ValueError, ctx.set_start_method, None)
58465846
self.check_context(ctx)
58475847

5848+
@staticmethod
5849+
def _dummy_func():
5850+
pass
5851+
5852+
def test_spawn_dont_set_context(self):
5853+
# Run a process with spawn or forkserver context may change
5854+
# the global start method, see gh-109263.
5855+
for method in ('fork', 'spawn', 'forkserver'):
5856+
multiprocessing.set_start_method(None, force=True)
5857+
5858+
try:
5859+
ctx = multiprocessing.get_context(method)
5860+
except ValueError:
5861+
continue
5862+
process = ctx.Process(target=self._dummy_func)
5863+
process.start()
5864+
process.join()
5865+
self.assertIsNone(multiprocessing.get_start_method(allow_none=True))
5866+
58485867
def test_context_check_module_types(self):
58495868
try:
58505869
ctx = multiprocessing.get_context('forkserver')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Starting a process from spawn context in :mod:`multiprocessing` no longer
2+
sets the start method globally.

0 commit comments

Comments
 (0)