GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it by albanD · Pull Request #107275 · python/cpython

gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it #107275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pitrou merged 5 commits into python:main from albanD:fix-issue-77377
Aug 23, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

gh-77377: Ensure multiprocessing SemLock is valid for spawn-based Process before serializing it #107275

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
d9aeedf
Ensure multiprocessing lock is valid for spawn Process before seriali…
albanD Jul 25, 2023
df69347
📜🤖 Added by blurb_it.
blurb-it[bot] Jul 25, 2023
152800c
Improve test to run on macos and cover forkserver
albanD Aug 23, 2023
29d37e1
Fix typos and wording
pitrou Aug 23, 2023
e5f4e32
lint
albanD Aug 23, 2023
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

9 changes: 7 additions & 2 deletions 9 Lib/multiprocessing/synchronize.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class SemLock(object):
def __init__(self, kind, value, maxvalue, *, ctx):
if ctx is None:
ctx = context._default_context.get_context()
name = ctx.get_start_method()
unlink_now = sys.platform == 'win32' or name == 'fork'
self.is_fork_ctx = ctx.get_start_method() == 'fork'
unlink_now = sys.platform == 'win32' or self.is_fork_ctx
for i in range(100):
try:
sl = self._semlock = _multiprocessing.SemLock(
Expand Down Expand Up @@ -103,6 +103,11 @@ def __getstate__(self):
if sys.platform == 'win32':
h = context.get_spawning_popen().duplicate_for_child(sl.handle)
else:
if self.is_fork_ctx:
raise RuntimeError('A SemLock created in a fork context is being '
'shared with a process in a spawn context. This is '
'not supported. Please use the same context to create '
'multiprocessing objects and Process.')
h = sl.handle
return (h, sl.kind, sl.maxvalue, sl.name)

Expand Down
22 changes: 22 additions & 0 deletions 22 Lib/test/_test_multiprocessing.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5421,6 +5421,28 @@ def test_preload_resources(self):
print(err)
self.fail("failed spawning forkserver or grandchild")

@unittest.skipIf(sys.platform == "win32",
"Only Spawn on windows so no risk of mixing")
@only_run_in_spawn_testsuite("avoids redundant testing.")
def test_mixed_startmethod(self):
# Fork-based locks cannot be used with spawned process
for process_method in ["spawn", "forkserver"]:
queue = multiprocessing.get_context("fork").Queue()
process_ctx = multiprocessing.get_context(process_method)
p = process_ctx.Process(target=close_queue, args=(queue,))
err_msg = "A SemLock created in a fork"
with self.assertRaisesRegex(RuntimeError, err_msg):
p.start()

# non-fork-based locks can be used with all other start methods
for queue_method in ["spawn", "forkserver"]:
for process_method in multiprocessing.get_all_start_methods():
queue = multiprocessing.get_context(queue_method).Queue()
process_ctx = multiprocessing.get_context(process_method)
p = process_ctx.Process(target=close_queue, args=(queue,))
p.start()
p.join()


@unittest.skipIf(sys.platform == "win32",
"test semantics don't make sense on Windows")
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Core and Builtins/2023-07-25-22-35-35.gh-issue-77377.EHAbXx.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +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.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.