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.
This commit is contained in:
Mose Müller
2025-06-19 09:59:08 +02:00
parent 9b57b6984e
commit 8bde104322

View File

@ -165,15 +165,16 @@ class SocketIOHandler(logging.Handler):
log_entry = self.format(record) log_entry = self.format(record)
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
loop.create_task( if loop.is_running():
self._sio.emit( loop.create_task(
"log", self._sio.emit(
{ "log",
"levelname": record.levelname, {
"message": log_entry, "levelname": record.levelname,
}, "message": log_entry,
},
)
) )
)
def setup_logging() -> None: def setup_logging() -> None: