Skip to content

Commit

Permalink
worker: fix deadlock when LoggingThread wrote into its own Queue
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Lukáš Zaoral <[email protected]>
  • Loading branch information
lzaoral and kdudka committed Mar 25, 2024
1 parent af73a03 commit 2072322
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions kobo/worker/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 2072322

Please sign in to comment.