Skip to content

Commit add8d45

Browse files
albanDblurb-it[bot]pitrou
authored
gh-108520: Fix bad fork detection in nested multiprocessing use case (#108568)
gh-107275 introduced a regression where a SemLock would fail being passed along nested child processes, as the `is_fork_ctx` attribute would be left missing after the first deserialization. --------- Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Antoine Pitrou <[email protected]>
1 parent 2a3926f commit add8d45

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

Lib/multiprocessing/synchronize.py

+5-3
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-
self.is_fork_ctx = ctx.get_start_method() == 'fork'
54-
unlink_now = sys.platform == 'win32' or self.is_fork_ctx
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,7 +103,7 @@ 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:
106+
if self._is_fork_ctx:
107107
raise RuntimeError('A SemLock created in a fork context is being '
108108
'shared with a process in a spawn context. This is '
109109
'not supported. Please use the same context to create '
@@ -115,6 +115,8 @@ def __setstate__(self, state):
115115
self._semlock = _multiprocessing.SemLock._rebuild(*state)
116116
util.debug('recreated blocker with handle %r' % state[0])
117117
self._make_methods()
118+
# Ensure that deserialized SemLock can be serialized again (gh-108520).
119+
self._is_fork_ctx = False
118120

119121
@staticmethod
120122
def _make_name():

Lib/test/_test_multiprocessing.py

+26
Original file line numberDiff line numberDiff line change
@@ -5443,6 +5443,32 @@ def test_mixed_startmethod(self):
54435443
p.start()
54445444
p.join()
54455445

5446+
@classmethod
5447+
def _put_one_in_queue(cls, queue):
5448+
queue.put(1)
5449+
5450+
@classmethod
5451+
def _put_two_and_nest_once(cls, queue):
5452+
queue.put(2)
5453+
process = multiprocessing.Process(target=cls._put_one_in_queue, args=(queue,))
5454+
process.start()
5455+
process.join()
5456+
5457+
def test_nested_startmethod(self):
5458+
# gh-108520: Regression test to ensure that child process can send its
5459+
# arguments to another process
5460+
queue = multiprocessing.Queue()
5461+
5462+
process = multiprocessing.Process(target=self._put_two_and_nest_once, args=(queue,))
5463+
process.start()
5464+
process.join()
5465+
5466+
results = []
5467+
while not queue.empty():
5468+
results.append(queue.get())
5469+
5470+
self.assertEqual(results, [2, 1])
5471+
54465472

54475473
@unittest.skipIf(sys.platform == "win32",
54485474
"test semantics don't make sense on Windows")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :meth:`multiprocessing.synchronize.SemLock.__setstate__` to properly initialize :attr:`multiprocessing.synchronize.SemLock._is_fork_ctx`. This fixes a regression when passing a SemLock accross nested processes.
2+
3+
Rename :attr:`multiprocessing.synchronize.SemLock.is_fork_ctx` to :attr:`multiprocessing.synchronize.SemLock._is_fork_ctx` to avoid exposing it as public API.

0 commit comments

Comments
 (0)