Skip to content

Commit bf0eed3

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 <[email protected]>
1 parent 9d409d6 commit bf0eed3

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
@@ -1230,13 +1230,15 @@ def is_active(self):
12301230

12311231
def close(self):
12321232
self._callbacks.clear()
1233-
if self._saved_sighandler is not None:
1234-
handler = signal.getsignal(signal.SIGCHLD)
1235-
if handler != self._sig_chld:
1236-
logger.warning("SIGCHLD handler was changed by outside code")
1237-
else:
1238-
signal.signal(signal.SIGCHLD, self._saved_sighandler)
1239-
self._saved_sighandler = None
1233+
if self._saved_sighandler is None:
1234+
return
1235+
1236+
handler = signal.getsignal(signal.SIGCHLD)
1237+
if handler != self._sig_chld:
1238+
logger.warning("SIGCHLD handler was changed by outside code")
1239+
else:
1240+
signal.signal(signal.SIGCHLD, self._saved_sighandler)
1241+
self._saved_sighandler = None
12401242

12411243
def __enter__(self):
12421244
return self
@@ -1263,15 +1265,17 @@ def attach_loop(self, loop):
12631265
# The reason to do it here is that attach_loop() is called from
12641266
# unix policy only for the main thread.
12651267
# Main thread is required for subscription on SIGCHLD signal
1268+
if self._saved_sighandler is not None:
1269+
return
1270+
1271+
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld)
12661272
if self._saved_sighandler is None:
1267-
self._saved_sighandler = signal.signal(signal.SIGCHLD, self._sig_chld)
1268-
if self._saved_sighandler is None:
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
1273+
logger.warning("Previous SIGCHLD handler was set by non-Python code, "
1274+
"restore to default handler on watcher close.")
1275+
self._saved_sighandler = signal.SIG_DFL
12721276

1273-
# Set SA_RESTART to limit EINTR occurrences.
1274-
signal.siginterrupt(signal.SIGCHLD, False)
1277+
# Set SA_RESTART to limit EINTR occurrences.
1278+
signal.siginterrupt(signal.SIGCHLD, False)
12751279

12761280
def _do_waitpid_all(self):
12771281
for pid in list(self._callbacks):

0 commit comments

Comments
 (0)