From 830949f67ab8c5db97684595b9c81625a47ed30a Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Wed, 8 May 2024 08:37:18 +0200
Subject: [PATCH] Don't break periodic tasks if they raise an exception

---
 .../tangostationcontrol/common/asyncio.py              | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/tangostationcontrol/tangostationcontrol/common/asyncio.py b/tangostationcontrol/tangostationcontrol/common/asyncio.py
index f716bc83a..5a61f4171 100644
--- a/tangostationcontrol/tangostationcontrol/common/asyncio.py
+++ b/tangostationcontrol/tangostationcontrol/common/asyncio.py
@@ -2,11 +2,14 @@
 # SPDX-License-Identifier: Apache-2.0
 
 import asyncio
+import logging
 from contextlib import suppress
 from concurrent.futures import Future, CancelledError
 from threading import Thread
 from typing import Callable
 
+logger = logging.getLogger()
+
 
 class EventLoopThread:
     """A forever running thread that keeps executing the given event_loop."""
@@ -88,7 +91,12 @@ class PeriodicTask:
 
     async def _call_periodically(self):
         while not self.done:
-            await self.func()
+            try:
+                await self.func()
+            except (CancelledError, asyncio.CancelledError):
+                raise
+            except Exception as ex:
+                logger.exception(f"Periodic task {self.func} raised an exception")
 
             # TODO(JDM): Calculate how long to sleep to have the runtime of
             #            func be subtracted.
-- 
GitLab