From b3aae5ef6fdbcd0663915ed324690fb864928029 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Fri, 28 Oct 2022 14:42:09 +0300 Subject: [PATCH] Fix asyncio child_watcher for Python 3.10 --- gobexec/asyncio/child_watcher.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gobexec/asyncio/child_watcher.py b/gobexec/asyncio/child_watcher.py index e6c75a2..655a127 100644 --- a/gobexec/asyncio/child_watcher.py +++ b/gobexec/asyncio/child_watcher.py @@ -2,16 +2,26 @@ import resource from asyncio import ThreadedChildWatcher from asyncio.log import logger -from asyncio.unix_events import _compute_returncode # based on https://www.enricozini.org/blog/2019/debian/getting-rusage-of-child-processes-on-python-s-asyncio/ from typing import Dict +# copied from unix_events.ThreadedChildWatcher on Python 3.10.6 +def waitstatus_to_exitcode(status): + try: + return os.waitstatus_to_exitcode(status) + except ValueError: + # The child exited, but we don't understand its status. + # This shouldn't happen, but if it does, let's just + # return that status; perhaps that helps debug it. + return status + + class RusageThreadedChildWatcher(ThreadedChildWatcher): rusages: Dict[int, resource.struct_rusage] = {} - # copied from unix_events.ThreadedChildWatcher on Python 3.9.7 + # copied from unix_events.ThreadedChildWatcher on Python 3.10.6 def _do_waitpid(self, loop, expected_pid, callback, args): assert expected_pid > 0 @@ -27,7 +37,7 @@ def _do_waitpid(self, loop, expected_pid, callback, args): "Unknown child process pid %d, will report returncode 255", pid) else: - returncode = _compute_returncode(status) + returncode = waitstatus_to_exitcode(status) if loop.get_debug(): logger.debug('process %s exited with returncode %s', expected_pid, returncode)