From 74b7860a0d795b6219ccf06f9d654c4438907efd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?= <cuu508@gmail.com>
Date: Fri, 14 Mar 2025 13:36:12 +0200
Subject: [PATCH] Fix exception logging in sendalerts

The on_notify_done callback was accessing exception data incorrectly.
If there's been an exception in the thread, it will be re-thrown while
calling future.result(), and we must catch it with "try ... except"
instead of calling future.exception().
---
 hc/api/management/commands/sendalerts.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/hc/api/management/commands/sendalerts.py b/hc/api/management/commands/sendalerts.py
index 1da885d6..d578a61b 100644
--- a/hc/api/management/commands/sendalerts.py
+++ b/hc/api/management/commands/sendalerts.py
@@ -79,10 +79,11 @@ class Command(BaseCommand):
     def on_notify_done(self, future: Future[str | None]) -> None:
         close_old_connections()
         self.seats.release()
-        if logs := future.result():
-            self.stdout.write(logs)
 
-        if exc := future.exception():
+        try:
+            if logs := future.result():
+                self.stdout.write(logs)
+        except Exception as exc:
             logger.error("Exception in notify", exc_info=exc)
             raise exc