Skip to content

Commit 66d3b58

Browse files
authored
bpo-38323: Add guard clauses in MultiLoopChildWatcher. (#22756)
This is a trivial refactor in preparation for a fix for bpo-38323.
1 parent c590c23 commit 66d3b58

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

Lib/asyncio/unix_events.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,13 +1226,15 @@ def is_active(self):
12261226

12271227
def close(self):
12281228
self._callbacks.clear()
1229-
if self._saved_sighandler is not None:
1230-
handler = signal.getsignal(signal.SIGCHLD)
1231-
if handler != self._sig_chld:
1232-
logger.warning("SIGCHLD handler was changed by outside code")
1233-
else:
1234-
signal.signal(signal.SIGCHLD, self._saved_sighandler)
1235-
self._saved_sighandler = None
1229+
if self._saved_sighandler is None:
1230+
return
1231+
1232+
handler = signal.getsignal(signal.SIGCHLD)
1233+
if handler != self._sig_chld:
1234+
logger.warning("SIGCHLD handler was changed by outside code")
1235+
else:
1236+
signal.signal(signal.SIGCHLD, self._saved_sighandler)
1237+
self._saved_sighandler = None
12361238

12371239
def __enter__(self):
12381240
return self
@@ -1259,15 +1261,17 @@ def attach_loop(self, loop):
12591261
# The reason to do it here is that attach_loop() is called from
12601262
# unix policy only for the main thread.
12611263
# Main thread is required for subscription on SIGCHLD signal
1264+
if self._saved_sighandler is not None:
1265+
return
1266+
1267+
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld)
12621268
if self._saved_sighandler is None:
1263-
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld)
1264-
if self._saved_sighandler is None:
1265-
logger.warning("Previous SIGCHLD handler was set by non-Python code, "
1266-
"restore to default handler on watcher close.")
1267-
self._saved_sighandler = signal.SIG_DFL
1269+
logger.warning("Previous SIGCHLD handler was set by non-Python code, "
1270+
"restore to default handler on watcher close.")
1271+
self._saved_sighandler = signal.SIG_DFL
12681272

1269-
# Set SA_RESTART to limit EINTR occurrences.
1270-
signal.siginterrupt(signal.SIGCHLD, False)
1273+
# Set SA_RESTART to limit EINTR occurrences.
1274+
signal.siginterrupt(signal.SIGCHLD, False)
12711275

12721276
def _do_waitpid_all(self):
12731277
for pid in list(self._callbacks):

0 commit comments

Comments
 (0)