Skip to content

Commit

Permalink
bpo-36813: Fix QueueListener to call task_done() upon termination. (G…
Browse files Browse the repository at this point in the history
…H-13113)

Fixed QueueListener in order to avoid random deadlocks.
Unable to add regression tests atm due to time constraints, will add it in a bit.
Regarding implementation, although it's nested, it does not cause performance issues whatsoever, and does not call task_done() in case of an exception (which is the right thing to do IMHO).


https://bugs.python.org/issue36813
  • Loading branch information
bharel authored and miss-islington committed Jun 1, 2019
1 parent 70c5f2a commit 6b282e1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lib/logging/handlers.py
Expand Up @@ -1477,6 +1477,8 @@ def _monitor(self):
try:
record = self.dequeue(True)
if record is self._sentinel:
if has_task_done:
q.task_done()
break
self.handle(record)
if has_task_done:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_logging.py
Expand Up @@ -3633,6 +3633,16 @@ def test_no_messages_in_queue_after_stop(self):
[m.msg if isinstance(m, logging.LogRecord)
else m for m in items]))

def test_calls_task_done_after_stop(self):
# Issue 36813: Make sure queue.join does not deadlock.
log_queue = queue.Queue()
listener = logging.handlers.QueueListener(log_queue)
listener.start()
listener.stop()
with self.assertRaises(ValueError):
# Make sure all tasks are done and .join won't block.
log_queue.task_done()


ZERO = datetime.timedelta(0)

Expand Down
@@ -0,0 +1,2 @@
Fix :class:`~logging.handlers.QueueListener` to call ``queue.task_done()``
upon stopping. Patch by Bar Harel.

0 comments on commit 6b282e1

Please sign in to comment.