Skip to content

Commit

Permalink
Assign return code before calling sys.excepthook()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DaanDeMeyer committed Jul 25, 2024
1 parent 4eba736 commit aed1a5c
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions mkosi/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit aed1a5c

Please sign in to comment.