mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
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 <pitrou@free.fr>
This commit is contained in:
parent
2a3926fa51
commit
add8d45cbe
3 changed files with 34 additions and 3 deletions
|
@ -50,8 +50,8 @@ class SemLock(object):
|
|||
def __init__(self, kind, value, maxvalue, *, ctx):
|
||||
if ctx is None:
|
||||
ctx = context._default_context.get_context()
|
||||
self.is_fork_ctx = ctx.get_start_method() == 'fork'
|
||||
unlink_now = sys.platform == 'win32' or self.is_fork_ctx
|
||||
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(
|
||||
|
@ -103,7 +103,7 @@ class SemLock(object):
|
|||
if sys.platform == 'win32':
|
||||
h = context.get_spawning_popen().duplicate_for_child(sl.handle)
|
||||
else:
|
||||
if self.is_fork_ctx:
|
||||
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 '
|
||||
|
@ -115,6 +115,8 @@ class SemLock(object):
|
|||
self._semlock = _multiprocessing.SemLock._rebuild(*state)
|
||||
util.debug('recreated blocker with handle %r' % state[0])
|
||||
self._make_methods()
|
||||
# Ensure that deserialized SemLock can be serialized again (gh-108520).
|
||||
self._is_fork_ctx = False
|
||||
|
||||
@staticmethod
|
||||
def _make_name():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue