8000 gh-108520: Fix bad fork detection in nested multiprocessing use case by albanD · Pull Request #108568 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-108520: Fix bad fork detection in nested multiprocessing use case #108568

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.

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
merged 9 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
8000 Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Support nesting and add test
  • Loading branch information
albanD committed Aug 29, 2023
commit ec4e7e0d32cf813b5fee8c086ec91ae1bda965fd
2 changes: 1 addition & 1 deletion Lib/multiprocessing/synchronize.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __getstate__(self):
if sys.platform == 'win32':
h = context.get_spawning_popen().duplicate_for_child(sl.handle)
else:
if self.is_fork_ctx:
if getattr(self, "is_fork_ctx", False):
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 '
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5342,6 +5342,16 @@ def test_ignore_listener(self):
finally:
conn.close()

# Utility functions used as target for spawn Process
def put_one_in_queue(queue):
queue.put(1)

def put_two_and_nest_once(queue):
queue.put(2)
process = multiprocessing.Process(target=put_one_in_queue, args=(queue,))
process.start()
process.join()

class TestStartMethod(unittest.TestCase):
@classmethod
def _check_context(cls, conn):
Expand Down Expand Up @@ -5443,6 +5453,19 @@ def test_mixed_startmethod(self):
p.start()
p.join()

def test_nested_startmethod(self):
queue = multiprocessing.Queue()

process = multiprocessing.Process(target=put_two_and_nest_once, args=(queue,))
process.start()
process.join()

results = []
while not queue.empty():
results.append(queue.get())

self.assertEqual(results, [2, 1])


@unittest.skipIf(sys.platform == "win32",
"test semantics don't make sense on Windows")
Expand Down
0