From: Daan De Meyer Date: Thu, 25 Jul 2024 07:15:13 +0000 (+0200) Subject: Assign return code before calling sys.excepthook() X-Git-Tag: v24~6^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aed1a5c45aaef4397aeb215bb116c7c228db7b48;p=thirdparty%2Fmkosi.git Assign return code before calling sys.excepthook() It seems sys.excepthook() can raise its own exception? I'm not entirely sure what's going on, but as a safety measure, let's assign the correct return code before we invoke sys.excepthook() so that we always exit with the right returncode. --- diff --git a/mkosi/run.py b/mkosi/run.py index da331abd4..d85928948 100644 --- a/mkosi/run.py +++ b/mkosi/run.py @@ -59,18 +59,21 @@ def uncaught_exception_handler(exit: Callable[[int], NoReturn] = sys.exit) -> It try: yield except SystemExit as e: + rc = e.code if isinstance(e.code, int) else 1 + if ARG_DEBUG.get(): sys.excepthook(*ensure_exc_info()) - - rc = e.code if isinstance(e.code, int) else 1 except KeyboardInterrupt: + rc = 1 + if ARG_DEBUG.get(): sys.excepthook(*ensure_exc_info()) else: logging.error("Interrupted") - - rc = 1 except subprocess.CalledProcessError as e: + # We always log when subprocess.CalledProcessError is raised, so we don't log again here. + rc = e.returncode + # Failures from qemu, ssh and systemd-nspawn are expected and we won't log stacktraces for those. # Failures from self come from the forks we spawn to build images in a user namespace. We've already done all # the logging for those failures so we don't log stacktraces for those either. @@ -81,9 +84,6 @@ def uncaught_exception_handler(exit: Callable[[int], NoReturn] = sys.exit) -> It not e.cmd[0].startswith("qemu") ): sys.excepthook(*ensure_exc_info()) - - # We always log when subprocess.CalledProcessError is raised, so we don't log again here. - rc = e.returncode except BaseException: sys.excepthook(*ensure_exc_info()) rc = 1