From 2072322cb10185c0b5e0319e51f20e84ce7bee0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Zaoral?= Date: Mon, 25 Mar 2024 16:36:42 +0100 Subject: [PATCH] worker: fix deadlock when LoggingThread wrote into its own Queue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If self._hub.upload_task_log() called self._queue.put(), it would cause deadlock because 1. self._queue uses locks that are not reentrant. 2. it will block if the Queue is already full. Co-authored-by: Kamil Dudka Co-authored-by: Lukáš Zaoral --- kobo/worker/logger.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/kobo/worker/logger.py b/kobo/worker/logger.py index 75b7a5b..c1a9208 100644 --- a/kobo/worker/logger.py +++ b/kobo/worker/logger.py @@ -88,8 +88,17 @@ def run(self): def write(self, data): """Add data to the queue and set the event for sending queue content.""" - self._queue.put(data) - self._event.set() + if threading.get_ident() != self.ident: + self._queue.put(data) + self._event.set() + + # If self._hub.upload_task_log() called self._queue.put(), it would + # cause deadlock because self._queue uses locks that are not reentrant + # and queue may already be full. + # + # Log only data with printable characters. + elif self._logger and data.strip(): + self._logger.log_error("Error in LoggingThread: %r", data) def stop(self): """Send remaining data to hub and finish."""