bpo-38323: Add guard clauses in MultiLoopChildWatcher. (#22756)

This is a trivial refactor in preparation for a fix for bpo-38323.
This commit is contained in:
Chris Jerdonek 2020-12-16 09:50:25 -08:00 committed by GitHub
parent c590c2338e
commit 66d3b589c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 15 deletions

View File

@ -1226,13 +1226,15 @@ class MultiLoopChildWatcher(AbstractChildWatcher):
def close(self): def close(self):
self._callbacks.clear() self._callbacks.clear()
if self._saved_sighandler is not None: if self._saved_sighandler is None:
handler = signal.getsignal(signal.SIGCHLD) return
if handler != self._sig_chld:
logger.warning("SIGCHLD handler was changed by outside code") handler = signal.getsignal(signal.SIGCHLD)
else: if handler != self._sig_chld:
signal.signal(signal.SIGCHLD, self._saved_sighandler) logger.warning("SIGCHLD handler was changed by outside code")
self._saved_sighandler = None else:
signal.signal(signal.SIGCHLD, self._saved_sighandler)
self._saved_sighandler = None
def __enter__(self): def __enter__(self):
return self return self
@ -1259,15 +1261,17 @@ class MultiLoopChildWatcher(AbstractChildWatcher):
# The reason to do it here is that attach_loop() is called from # The reason to do it here is that attach_loop() is called from
# unix policy only for the main thread. # unix policy only for the main thread.
# Main thread is required for subscription on SIGCHLD signal # Main thread is required for subscription on SIGCHLD signal
if self._saved_sighandler is None: if self._saved_sighandler is not None:
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld) return
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
# Set SA_RESTART to limit EINTR occurrences. self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld)
signal.siginterrupt(signal.SIGCHLD, False) 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
# Set SA_RESTART to limit EINTR occurrences.
signal.siginterrupt(signal.SIGCHLD, False)
def _do_waitpid_all(self): def _do_waitpid_all(self):
for pid in list(self._callbacks): for pid in list(self._callbacks):