From c2c4e9c42e5f29d76b035ebc8aad80b790fbc352 Mon Sep 17 00:00:00 2001 From: Jordan Ruthe Date: Sun, 2 May 2021 17:48:04 -0400 Subject: [PATCH] function: Add except hook for threads This will log exceptions on different threads --- ks_includes/functions.py | 24 +++++++++++++++++++++++- screen.py | 2 ++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ks_includes/functions.py b/ks_includes/functions.py index f4ec7fdb..bf2c0873 100644 --- a/ks_includes/functions.py +++ b/ks_includes/functions.py @@ -1,8 +1,11 @@ import logging import logging.handlers import os +import re import subprocess import sys +import threading +import time import traceback from queue import SimpleQueue as Queue @@ -66,6 +69,24 @@ def get_software_version(): logging.exception("Error runing git describe") return "?" +def patch_threading_excepthook(): + """Installs our exception handler into the threading modules Thread object + Inspired by https://bugs.python.org/issue1230540 + """ + old_init = threading.Thread.__init__ + def new_init(self, *args, **kwargs): + old_init(self, *args, **kwargs) + old_run = self.run + def run_with_excepthook(*args, **kwargs): + try: + old_run(*args, **kwargs) + except (KeyboardInterrupt, SystemExit): + raise + except: + sys.excepthook(*sys.exc_info(), thread_identifier=threading.get_ident()) + self.run = run_with_excepthook + threading.Thread.__init__ = new_init + # Timed rotating file handler based on Klipper and Moonraker's implementation class KlipperScreenLoggingHandler(logging.handlers.TimedRotatingFileHandler): def __init__(self, software_version, filename, **kwargs): @@ -113,8 +134,9 @@ def setup_logging(log_file, software_version): queue, stdout_hdlr) listener.start() - def logging_exception_handler(type, value, tb): + def logging_exception_handler(type, value, tb, thread_identifier=None): logging.exception("Uncaught exception %s: %s\nTraceback: %s" % (type, value, "\n".join(traceback.format_tb(tb)))) sys.excepthook = logging_exception_handler + logging.captureWarnings(True) return listener, fh diff --git a/screen.py b/screen.py index 07ad33ab..74638b8b 100644 --- a/screen.py +++ b/screen.py @@ -745,6 +745,8 @@ def main(): version ) + functions.patch_threading_excepthook() + logging.info("KlipperScreen version: %s" % version)