Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Lib/asyncio/unix_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,9 +930,20 @@ def add_child_handler(self, pid, callback, *args):
def _do_wait(self, pid):
pidfd, callback, args = self._callbacks.pop(pid)
self._loop._remove_reader(pidfd)
_, status = os.waitpid(pid, 0)
try:
_, status = os.waitpid(pid, 0)
except ChildProcessError:
# The child process is already reaped
# (may happen if waitpid() is called elsewhere).
returncode = 255
logger.warning(
"child process pid %d exit status already read: "
" will report returncode 255",
pid)
else:
returncode = _compute_returncode(status)

os.close(pidfd)
returncode = _compute_returncode(status)
callback(pid, returncode, *args)

def remove_child_handler(self, pid):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix asyncio ``PidfdChildWatcher``: handle ``waitpid()`` error. If
``waitpid()`` is called elsewhere, ``waitpid()`` call fails with
:exc:`ChildProcessError`: use return code 255 in this case, and log a
warning. It ensures that the pidfd file descriptor is closed if this error
occurs.