Skip to content

Commit 6b282e1

Browse files
bharelmiss-islington
authored andcommitted
bpo-36813: Fix QueueListener to call task_done() upon termination. (GH-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
1 parent 70c5f2a commit 6b282e1

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

Lib/logging/handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,6 +1477,8 @@ def _monitor(self):
14771477
try:
14781478
record = self.dequeue(True)
14791479
if record is self._sentinel:
1480+
if has_task_done:
1481+
q.task_done()
14801482
break
14811483
self.handle(record)
14821484
if has_task_done:

Lib/test/test_logging.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3633,6 +3633,16 @@ def test_no_messages_in_queue_after_stop(self):
36333633
[m.msg if isinstance(m, logging.LogRecord)
36343634
else m for m in items]))
36353635

3636+
def test_calls_task_done_after_stop(self):
3637+
# Issue 36813: Make sure queue.join does not deadlock.
3638+
log_queue = queue.Queue()
3639+
listener = logging.handlers.QueueListener(log_queue)
3640+
listener.start()
3641+
listener.stop()
3642+
with self.assertRaises(ValueError):
3643+
# Make sure all tasks are done and .join won't block.
3644+
log_queue.task_done()
3645+
36363646

36373647
ZERO = datetime.timedelta(0)
36383648

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :class:`~logging.handlers.QueueListener` to call ``queue.task_done()``
2+
upon stopping. Patch by Bar Harel.

0 commit comments

Comments
 (0)