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

Commit1700d34

Browse files
albanDblurb-it[bot]pitrou
authored
gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it (#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.---------Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>Co-authored-by: Antoine Pitrou <pitrou@free.fr>
1 parent5d18715 commit1700d34

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

‎Lib/multiprocessing/synchronize.py‎

Lines changed: 7 additions & 2 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-
name=ctx.get_start_method()
54-
unlink_now=sys.platform=='win32'orname=='fork'
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,6 +103,11 @@ 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:
107+
raiseRuntimeError('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‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5421,6 +5421,28 @@ def test_preload_resources(self):
54215421
print(err)
54225422
self.fail("failed spawning forkserver or grandchild")
54235423

5424+
@unittest.skipIf(sys.platform=="win32",
5425+
"Only Spawn on windows so no risk of mixing")
5426+
@only_run_in_spawn_testsuite("avoids redundant testing.")
5427+
deftest_mixed_startmethod(self):
5428+
# Fork-based locks cannot be used with spawned process
5429+
forprocess_methodin ["spawn","forkserver"]:
5430+
queue=multiprocessing.get_context("fork").Queue()
5431+
process_ctx=multiprocessing.get_context(process_method)
5432+
p=process_ctx.Process(target=close_queue,args=(queue,))
5433+
err_msg="A SemLock created in a fork"
5434+
withself.assertRaisesRegex(RuntimeError,err_msg):
5435+
p.start()
5436+
5437+
# non-fork-based locks can be used with all other start methods
5438+
forqueue_methodin ["spawn","forkserver"]:
5439+
forprocess_methodinmultiprocessing.get_all_start_methods():
5440+
queue=multiprocessing.get_context(queue_method).Queue()
5441+
process_ctx=multiprocessing.get_context(process_method)
5442+
p=process_ctx.Process(target=close_queue,args=(queue,))
5443+
p.start()
5444+
p.join()
5445+
54245446

54255447
@unittest.skipIf(sys.platform=="win32",
54265448
"test semantics don't make sense on Windows")
Lines changed: 1 addition & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp