Skip to content

Commit dd262ef

Browse files
bpo-38323: Add guard clauses in MultiLoopChildWatcher. (GH-22756)
This is a trivial refactor in preparation for a fix for bpo-38323. (cherry picked from commit 66d3b58) Co-authored-by: Chris Jerdonek <chris.jerdonek@gmail.com>
1 parent d549d0b commit dd262ef

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

Lib/asyncio/unix_events.py

+18-14
Original file line numberDiff line numberDiff line change
@@ -1152,13 +1152,15 @@ def is_active(self):
11521152

11531153
def close(self):
11541154
self._callbacks.clear()
1155-
if self._saved_sighandler is not None:
1156-
handler = signal.getsignal(signal.SIGCHLD)
1157-
if handler != self._sig_chld:
1158-
logger.warning("SIGCHLD handler was changed by outside code")
1159-
else:
1160-
signal.signal(signal.SIGCHLD, self._saved_sighandler)
1161-
self._saved_sighandler = None
1155+
if self._saved_sighandler is None:
1156+
return
1157+
1158+
handler = signal.getsignal(signal.SIGCHLD)
1159+
if handler != self._sig_chld:
1160+
logger.warning("SIGCHLD handler was changed by outside code")
1161+
else:
1162+
signal.signal(signal.SIGCHLD, self._saved_sighandler)
1163+
self._saved_sighandler = None
11621164

11631165
def __enter__(self):
11641166
return self
@@ -1185,15 +1187,17 @@ def attach_loop(self, loop):
11851187
# The reason to do it here is that attach_loop() is called from
11861188
# unix policy only for the main thread.
11871189
# Main thread is required for subscription on SIGCHLD signal
1190+
if self._saved_sighandler is not None:
1191+
return
1192+
1193+
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld)
11881194
if self._saved_sighandler is None:
1189-
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld)
1190-
if self._saved_sighandler is None:
1191-
logger.warning("Previous SIGCHLD handler was set by non-Python code, "
1192-
"restore to default handler on watcher close.")
1193-
self._saved_sighandler = signal.SIG_DFL
1195+
logger.warning("Previous SIGCHLD handler was set by non-Python code, "
1196+
"restore to default handler on watcher close.")
1197+
self._saved_sighandler = signal.SIG_DFL
11941198

1195-
# Set SA_RESTART to limit EINTR occurrences.
1196-
signal.siginterrupt(signal.SIGCHLD, False)
1199+
# Set SA_RESTART to limit EINTR occurrences.
1200+
signal.siginterrupt(signal.SIGCHLD, False)
11971201

11981202
def _do_waitpid_all(self):
11991203
for pid in list(self._callbacks):

0 commit comments

Comments
 (0)