From 8bde104322cff74dc30ff6f626d8e0a16a7acce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mose=20M=C3=BCller?= Date: Thu, 19 Jun 2025 09:59:08 +0200 Subject: [PATCH] fix: check if loop is running in SocketIOHandler Before emitting sio events in the SocketIOHandler, I have to check if the loop is actually still running. This caused issues with pytest as pytest was tearing down asyncio tasks and stopping the loop, while the sio handler was still trying to send those logs to the sio clients. --- src/pydase/utils/logging.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/pydase/utils/logging.py b/src/pydase/utils/logging.py index 32ba1c2..4b86221 100644 --- a/src/pydase/utils/logging.py +++ b/src/pydase/utils/logging.py @@ -165,15 +165,16 @@ class SocketIOHandler(logging.Handler): log_entry = self.format(record) loop = asyncio.get_event_loop() - loop.create_task( - self._sio.emit( - "log", - { - "levelname": record.levelname, - "message": log_entry, - }, + if loop.is_running(): + loop.create_task( + self._sio.emit( + "log", + { + "levelname": record.levelname, + "message": log_entry, + }, + ) ) - ) def setup_logging() -> None: