Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-130895: fix multiprocessing.Process join/wait/poll races#131440
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
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
a1408a4ad102f487f391f6ff7c045b4dbb0e7f71445d87b96a8f70fbFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import atexit | ||
| import os | ||
| import signal | ||
| import threading | ||
| from . import util | ||
| @@ -17,23 +18,74 @@ def __init__(self, process_obj): | ||
| util._flush_std_streams() | ||
| self.returncode = None | ||
| self.finalizer = None | ||
| self._init_locking() | ||
| self._launch(process_obj) | ||
| def _init_locking(self): | ||
| self._exit_condition = threading.Condition() | ||
| self._exit_blockers = 0 | ||
| def duplicate_for_child(self, fd): | ||
| return fd | ||
| def poll(self, flag=os.WNOHANG): | ||
| if self.returncode is None: | ||
| try: | ||
| pid, sts = os.waitpid(self.pid, flag) | ||
| except OSError: | ||
| # Child process not yet created. See #1731717 | ||
| # e.errno == errno.ECHILD == 10 | ||
| return None | ||
| with self._exit_condition: | ||
| if self.returncode is not None: | ||
| return self.returncode | ||
| elif flag & os.WNOHANG == os.WNOHANG: | ||
| return self._nonblocking_poll(flag) | ||
| else: | ||
| self._exit_blockers += 1 | ||
| # We have released the lock, so may be racing with blocking & | ||
| # non-blocking calls at this point... | ||
| pid = None | ||
| try: | ||
| pid, sts = os.waitpid(self.pid, flag) | ||
| except OSError: | ||
| # Child process doesn't exist because it hasn't started yet (see | ||
| # bpo-1731717) or has already been awaited on a racing thread (see | ||
| # gh-130895) | ||
| pass | ||
| with self._exit_condition: | ||
| self._exit_blockers -= 1 | ||
| if pid == self.pid: | ||
duaneg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self.returncode = os.waitstatus_to_exitcode(sts) | ||
| self._set_returncode(sts) | ||
| elif self._exit_blockers == 0: | ||
| self._exit_condition.notify_all() | ||
| # Wait until we get a definitive result, or we know there are no | ||
| # racing calls that might be about to set it | ||
| while self.returncode is None and self._exit_blockers > 0: | ||
| self._exit_condition.wait() | ||
duaneg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return self.returncode | ||
| def _nonblocking_poll(self, flag): | ||
| assert self._exit_condition._is_owned() | ||
| assert self.returncode is None | ||
| assert flag & os.WNOHANG == os.WNOHANG | ||
| try: | ||
| pid, sts = os.waitpid(self.pid, flag) | ||
| if pid == self.pid: | ||
| self._set_returncode(sts) | ||
| except OSError: | ||
| # See comments in the poll(...) except clause above | ||
| pass | ||
| # We may be racing with a blocking wait call, in which case (if we lose | ||
| # the race) it is arbitrary whether this returns None or the exit code | ||
| # (if there is one): calling code must always be prepared to handle a | ||
| # situation where this method returns None but the process has ended. | ||
| return self.returncode | ||
| def _set_returncode(self, sts): | ||
| assert self._exit_condition._is_owned() | ||
| assert self.returncode is None | ||
| self.returncode = os.waitstatus_to_exitcode(sts) | ||
| self._exit_condition.notify_all() | ||
| def wait(self, timeout=None): | ||
| if self.returncode is None: | ||
| if timeout is not None: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix race with ``poll``/``wait``/``join`` in :mod:`multiprocessing`.``Process``. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.