Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5121deb

Browse files
committed
Issue #19310: asyncio: fix child processes reaping logic.
1 parent e5a3154 commit 5121deb

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

Lib/asyncio/unix_events.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,25 @@ def _reg_sigchld(self):
167167

168168
def _sig_chld(self):
169169
try:
170-
try:
171-
pid, status = os.waitpid(-1, os.WNOHANG)
172-
except ChildProcessError:
173-
return
174-
if pid == 0:
175-
self.call_soon(self._sig_chld)
176-
return
177-
elif os.WIFSIGNALED(status):
178-
returncode = -os.WTERMSIG(status)
179-
elif os.WIFEXITED(status):
180-
returncode = os.WEXITSTATUS(status)
181-
else:
182-
self.call_soon(self._sig_chld)
183-
return
184-
transp = self._subprocesses.get(pid)
185-
if transp is not None:
186-
transp._process_exited(returncode)
170+
# because of signal coalescing, we must keep calling waitpid() as
171+
# long as we're able to reap a child
172+
while True:
173+
try:
174+
pid, status = os.waitpid(-1, os.WNOHANG)
175+
except ChildProcessError:
176+
break
177+
if pid == 0:
178+
break
179+
elif os.WIFSIGNALED(status):
180+
returncode = -os.WTERMSIG(status)
181+
elif os.WIFEXITED(status):
182+
returncode = os.WEXITSTATUS(status)
183+
else:
184+
# shouldn't happen
185+
continue
186+
transp = self._subprocesses.get(pid)
187+
if transp is not None:
188+
transp._process_exited(returncode)
187189
except Exception:
188190
logger.exception('Unknown exception in SIGCHLD handler')
189191

0 commit comments

Comments
 (0)