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

Skip to content

Commit e76ee1a

Browse files
authored
bpo-38982: Fix asyncio PidfdChildWatcher on waitpid() error (GH-17477)
If waitpid() is called elsewhere, waitpid() call fails with ChildProcessError: use return code 255 in this case, and log a warning. It ensure that the pidfd file descriptor is closed if this error occurs.
1 parent b64334c commit e76ee1a

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

Lib/asyncio/unix_events.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,20 @@ def add_child_handler(self, pid, callback, *args):
930930
def _do_wait(self, pid):
931931
pidfd, callback, args = self._callbacks.pop(pid)
932932
self._loop._remove_reader(pidfd)
933-
_, status = os.waitpid(pid, 0)
933+
try:
934+
_, status = os.waitpid(pid, 0)
935+
except ChildProcessError:
936+
# The child process is already reaped
937+
# (may happen if waitpid() is called elsewhere).
938+
returncode = 255
939+
logger.warning(
940+
"child process pid %d exit status already read: "
941+
" will report returncode 255",
942+
pid)
943+
else:
944+
returncode = _compute_returncode(status)
945+
934946
os.close(pidfd)
935-
returncode = _compute_returncode(status)
936947
callback(pid, returncode, *args)
937948

938949
def remove_child_handler(self, pid):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix asyncio ``PidfdChildWatcher``: handle ``waitpid()`` error. If
2+
``waitpid()`` is called elsewhere, ``waitpid()`` call fails with
3+
:exc:`ChildProcessError`: use return code 255 in this case, and log a
4+
warning. It ensures that the pidfd file descriptor is closed if this error
5+
occurs.

0 commit comments

Comments
 (0)