Skip to content

Commit 34ef75d

Browse files
miss-islingtonalbanDblurb-it[bot]pitrou
authored
[3.11] gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it (GH-107275) (#108378)
gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it (GH-107275) Ensure multiprocessing SemLock is valid for spawn Process before serializing it. Creating a multiprocessing SemLock with a fork context, and then trying to pass it to a spawn-created Process, would segfault if not detected early. --------- (cherry picked from commit 1700d34) Co-authored-by: albanD <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Antoine Pitrou <[email protected]>
1 parent 93714b7 commit 34ef75d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Lib/multiprocessing/synchronize.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class SemLock(object):
5050
def __init__(self, kind, value, maxvalue, *, ctx):
5151
if ctx is None:
5252
ctx = context._default_context.get_context()
53-
name = ctx.get_start_method()
54-
unlink_now = sys.platform == 'win32' or name == 'fork'
53+
self.is_fork_ctx = ctx.get_start_method() == 'fork'
54+
unlink_now = sys.platform == 'win32' or self.is_fork_ctx
5555
for i in range(100):
5656
try:
5757
sl = self._semlock = _multiprocessing.SemLock(
@@ -103,6 +103,11 @@ def __getstate__(self):
103103
if sys.platform == 'win32':
104104
h = context.get_spawning_popen().duplicate_for_child(sl.handle)
105105
else:
106+
if self.is_fork_ctx:
107+
raise RuntimeError('A SemLock created in a fork context is being '
108+
'shared with a process in a spawn context. This is '
109+
'not supported. Please use the same context to create '
110+
'multiprocessing objects and Process.')
106111
h = sl.handle
107112
return (h, sl.kind, sl.maxvalue, sl.name)
108113

Lib/test/_test_multiprocessing.py

+22
Original file line numberDiff line numberDiff line change
@@ -5345,6 +5345,28 @@ def test_preload_resources(self):
53455345
print(err)
53465346
self.fail("failed spawning forkserver or grandchild")
53475347

5348+
@unittest.skipIf(sys.platform == "win32",
5349+
"Only Spawn on windows so no risk of mixing")
5350+
@only_run_in_spawn_testsuite("avoids redundant testing.")
5351+
def test_mixed_startmethod(self):
5352+
# Fork-based locks cannot be used with spawned process
5353+
for process_method in ["spawn", "forkserver"]:
5354+
queue = multiprocessing.get_context("fork").Queue()
5355+
process_ctx = multiprocessing.get_context(process_method)
5356+
p = process_ctx.Process(target=close_queue, args=(queue,))
5357+
err_msg = "A SemLock created in a fork"
5358+
with self.assertRaisesRegex(RuntimeError, err_msg):
5359+
p.start()
5360+
5361+
# non-fork-based locks can be used with all other start methods
5362+
for queue_method in ["spawn", "forkserver"]:
5363+
for process_method in multiprocessing.get_all_start_methods():
5364+
queue = multiprocessing.get_context(queue_method).Queue()
5365+
process_ctx = multiprocessing.get_context(process_method)
5366+
p = process_ctx.Process(target=close_queue, args=(queue,))
5367+
p.start()
5368+
p.join()
5369+
53485370

53495371
@unittest.skipIf(sys.platform == "win32",
53505372
"test semantics don't make sense on Windows")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure that multiprocessing synchronization objects created in a fork context are not sent to a different process created in a spawn context. This changes a segfault into an actionable RuntimeError in the parent process.

0 commit comments

Comments
 (0)