-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
[WIP] bpo-38323: Fix MultiLoopChildWatcher hangs #20142
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d10132
bpo-38323: Fix rare MultiLoopChildWatcher hangs.
cjerdonek 9618884
Add docstring.
cjerdonek 4d4c147
Address a couple review comments.
cjerdonek 14f6cfc
Revert tearDown() change.
cjerdonek 22ba0b1
Tweak documentation change.
cjerdonek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,8 @@ def _process_self_data(self, data): | |
def add_signal_handler(self, sig, callback, *args): | ||
"""Add a handler for a signal. UNIX only. | ||
|
||
This method can only be called from the main thread. | ||
|
||
Raise ValueError if the signal number is invalid or uncatchable. | ||
Raise RuntimeError if there is a problem setting up the handler. | ||
""" | ||
|
@@ -1232,10 +1234,15 @@ def close(self): | |
self._callbacks.clear() | ||
if self._saved_sighandler is not None: | ||
handler = signal.getsignal(signal.SIGCHLD) | ||
if handler != self._sig_chld: | ||
# add_signal_handler() sets the handler to _sighandler_noop. | ||
if handler != _sighandler_noop: | ||
logger.warning("SIGCHLD handler was changed by outside code") | ||
else: | ||
loop = self._loop | ||
# This clears the wakeup file descriptor if necessary. | ||
loop.remove_signal_handler(signal.SIGCHLD) | ||
signal.signal(signal.SIGCHLD, self._saved_sighandler) | ||
|
||
self._saved_sighandler = None | ||
|
||
def __enter__(self): | ||
|
@@ -1259,19 +1266,33 @@ def remove_child_handler(self, pid): | |
return False | ||
|
||
def attach_loop(self, loop): | ||
""" | ||
This registers the SIGCHLD signal handler. | ||
|
||
This method can only be called from the main thread. | ||
""" | ||
# Don't save the loop but initialize itself if called first time | ||
# The reason to do it here is that attach_loop() is called from | ||
# unix policy only for the main thread. | ||
# Main thread is required for subscription on SIGCHLD signal | ||
if loop is None or self._saved_sighandler is not None: | ||
return | ||
|
||
self._loop = loop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Part of why I was saying to remove this line is to honor the code comment 7 lines above: # Don't save the loop but initialize itself if called first time |
||
self._saved_sighandler = signal.getsignal(signal.SIGCHLD) | ||
if self._saved_sighandler is None: | ||
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld) | ||
if self._saved_sighandler is None: | ||
logger.warning("Previous SIGCHLD handler was set by non-Python code, " | ||
"restore to default handler on watcher close.") | ||
self._saved_sighandler = signal.SIG_DFL | ||
logger.warning("Previous SIGCHLD handler was set by non-Python code, " | ||
"restore to default handler on watcher close.") | ||
self._saved_sighandler = signal.SIG_DFL | ||
|
||
# Set SA_RESTART to limit EINTR occurrences. | ||
signal.siginterrupt(signal.SIGCHLD, False) | ||
if self._callbacks: | ||
warnings.warn( | ||
'A loop is being detached ' | ||
'from a child watcher with pending handlers', | ||
RuntimeWarning) | ||
|
||
# This also sets up the wakeup file descriptor. | ||
loop.add_signal_handler(signal.SIGCHLD, self._sig_chld) | ||
|
||
def _do_waitpid_all(self): | ||
for pid in list(self._callbacks): | ||
|
@@ -1314,7 +1335,7 @@ def _do_waitpid(self, expected_pid): | |
expected_pid, returncode) | ||
loop.call_soon_threadsafe(callback, pid, returncode, *args) | ||
|
||
def _sig_chld(self, signum, frame): | ||
def _sig_chld(self, *args): | ||
try: | ||
self._do_waitpid_all() | ||
except (SystemExit, KeyboardInterrupt): | ||
|
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2020-05-16-17-50-10.bpo-38323.Ar35np.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix rare cases with :class:`asyncio.MultiLoopChildWatcher` where the event | ||
loop can fail to awaken in response to a :py:data:`SIGCHLD` signal. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aeros I would think through what should happen if either
loop is None
orself._saved_sighandler is not None
. For example, the previous implementation didn't use theloop
argument, so there is a risk that code will stop working for people who were passingloop=None
.