Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitadd8d45

Browse files
albanDblurb-it[bot]pitrou
authored
pythongh-108520: Fix bad fork detection in nested multiprocessing use case (python#108568)
pythongh-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 <pitrou@free.fr>
1 parent2a3926f commitadd8d45

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

‎Lib/multiprocessing/synchronize.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class SemLock(object):
5050
def__init__(self,kind,value,maxvalue,*,ctx):
5151
ifctxisNone:
5252
ctx=context._default_context.get_context()
53-
self.is_fork_ctx=ctx.get_start_method()=='fork'
54-
unlink_now=sys.platform=='win32'orself.is_fork_ctx
53+
self._is_fork_ctx=ctx.get_start_method()=='fork'
54+
unlink_now=sys.platform=='win32'orself._is_fork_ctx
5555
foriinrange(100):
5656
try:
5757
sl=self._semlock=_multiprocessing.SemLock(
@@ -103,7 +103,7 @@ def __getstate__(self):
103103
ifsys.platform=='win32':
104104
h=context.get_spawning_popen().duplicate_for_child(sl.handle)
105105
else:
106-
ifself.is_fork_ctx:
106+
ifself._is_fork_ctx:
107107
raiseRuntimeError('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‎

Lines changed: 26 additions & 0 deletions
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+
deftest_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+
whilenotqueue.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")
Lines changed: 3 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp