From 59fa533aef61b07b8a7b1186731b319c17aa5e58 Mon Sep 17 00:00:00 2001
From: rdbende <rdbende@proton.me>
Date: Mon, 4 Dec 2023 21:40:44 +0100
Subject: [PATCH] Excepthook workaround is no longer needed

---
 cozy/application.py | 44 ++++++++++----------------------------------
 1 file changed, 10 insertions(+), 34 deletions(-)

diff --git a/cozy/application.py b/cozy/application.py
index 59996a49..fee6d702 100644
--- a/cozy/application.py
+++ b/cozy/application.py
@@ -22,32 +22,6 @@
 log = logging.getLogger("application")
 
 
-def setup_thread_excepthook():
-    """
-    Workaround for `sys.excepthook` thread bug from:
-    http://bugs.python.org/issue1230540
-
-    Call once from the main thread before creating any threads.
-    """
-
-    init_original = threading.Thread.__init__
-
-    def init(self, *args, **kwargs):
-
-        init_original(self, *args, **kwargs)
-        run_original = self.run
-
-        def run_with_except_hook(*args2, **kwargs2):
-            try:
-                run_original(*args2, **kwargs2)
-            except Exception:
-                sys.excepthook(*sys.exc_info())
-
-        self.run = run_with_except_hook
-
-    threading.Thread.__init__ = init
-
-
 class Application(Adw.Application):
     ui: CozyUI
     app_controller: AppController
@@ -61,9 +35,7 @@ def __init__(self, pkgdatadir: str):
         GLib.setenv("PULSE_PROP_media.role", "music", True)
         GLib.set_application_name("Cozy")
 
-        self.old_except_hook = sys.excepthook
-        sys.excepthook = self.handle_exception
-        setup_thread_excepthook()
+        threading.excepthook = self.handle_exception
 
     def do_startup(self):
         log.info(distro.linux_distribution(full_distribution_name=False))
@@ -95,14 +67,18 @@ def do_activate(self):
             mpris = MPRIS(self)
             mpris._on_current_changed()
 
-    def handle_exception(self, exc_type, exc_value, exc_traceback):
+    def handle_exception(self, _):
         print("handle exception")
+
+        exc_type, exc_value, exc_traceback = sys.exc_info()
+
+        if exc_type is SystemExit:
+            return
+
         try:
             reporter.exception("uncaught", exc_value, "\n".join(format_exception(exc_type, exc_value, exc_traceback)))
-        except Exception:
-            None
-
-        self.old_except_hook(exc_type, exc_value, exc_traceback)
+        finally:
+            sys.excepthook(exc_type, exc_value, exc_traceback)
 
     def quit(self):
         self.app_controller.quit()