From 8409b2a5bb3538eaf651f0cc80b0a0f015fbad3e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 5 Apr 2020 16:39:33 +0300 Subject: [PATCH] Try to make log collector thread safe --- maubot/__main__.py | 6 +++--- maubot/management/api/log.py | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/maubot/__main__.py b/maubot/__main__.py index 412201c..a9939a6 100644 --- a/maubot/__main__.py +++ b/maubot/__main__.py @@ -44,17 +44,17 @@ config.update() logging.config.dictConfig(copy.deepcopy(config["logging"])) +loop = asyncio.get_event_loop() + stop_log_listener = None if config["api_features.log"]: from .management.api.log import init as init_log_listener, stop_all as stop_log_listener - init_log_listener() + init_log_listener(loop) log = logging.getLogger("maubot.init") log.info(f"Initializing maubot {__version__}") -loop = asyncio.get_event_loop() - init_zip_loader(config) db_engine = init_db(config) clients = init_client_class(loop) diff --git a/maubot/management/api/log.py b/maubot/management/api/log.py index d4e78cc..d6ec092 100644 --- a/maubot/management/api/log.py +++ b/maubot/management/api/log.py @@ -38,6 +38,7 @@ class LogCollector(logging.Handler): lines: Deque[dict] formatter: logging.Formatter listeners: List[web.WebSocketResponse] + loop: asyncio.AbstractEventLoop def __init__(self, level=logging.NOTSET) -> None: super().__init__(level) @@ -69,7 +70,7 @@ class LogCollector(logging.Handler): for name, value in content.items(): if isinstance(value, datetime): content[name] = value.astimezone().isoformat() - asyncio.ensure_future(self.send(content)) + asyncio.run_coroutine_threadsafe(self.send(content), loop=self.loop) self.lines.append(content) async def send(self, record: dict) -> None: @@ -86,8 +87,9 @@ log = logging.getLogger("maubot.server.websocket") sockets = [] -def init() -> None: +def init(loop: asyncio.AbstractEventLoop) -> None: log_root.addHandler(handler) + handler.loop = loop async def stop_all() -> None: