From 2bc15019f7caf080192beafff6385e2deeffaa6e Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Sat, 6 Apr 2019 23:15:20 +0200 Subject: [PATCH] Fix: use a mutex when printing to the console The debug logging and the server logging may happen from different threads. So use a print mutex as a simple solution. --- plugin/core/logging.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/plugin/core/logging.py b/plugin/core/logging.py index 4fcd8a245..1e3bfa2b3 100644 --- a/plugin/core/logging.py +++ b/plugin/core/logging.py @@ -1,7 +1,9 @@ import traceback +from threading import Lock log_debug = False log_exceptions = True +mutex = Lock() def set_debug_logging(logging_enabled: bool) -> None: @@ -17,18 +19,21 @@ def set_exception_logging(logging_enabled: bool) -> None: def debug(*args): """Print args to the console if the "debug" setting is True.""" if log_debug: - printf(*args) + with mutex: + printf(*args) def exception_log(message: str, ex) -> None: if log_exceptions: - print(message) - ex_traceback = ex.__traceback__ - print(''.join(traceback.format_exception(ex.__class__, ex, ex_traceback))) + with mutex: + print(message) + ex_traceback = ex.__traceback__ + print(''.join(traceback.format_exception(ex.__class__, ex, ex_traceback))) def server_log(*args) -> None: - printf(*args, prefix="server") + with mutex: + printf(*args, prefix="server") def printf(*args, prefix='LSP'):