]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36813: Fix QueueListener to call task_done() upon termination. (GH-13113)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 1 Jun 2019 09:36:29 +0000 (02:36 -0700)
committerGitHub <noreply@github.com>
Sat, 1 Jun 2019 09:36:29 +0000 (02:36 -0700)
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
(cherry picked from commit 6b282e18877ec84e927b381b4ce187eaf4ba3dd7)

Co-authored-by: Bar Harel <bzvi7919@gmail.com>
Lib/logging/handlers.py
Lib/test/test_logging.py
Misc/NEWS.d/next/Library/2019-05-06-18-28-38.bpo-36813.NXD0KZ.rst [new file with mode: 0644]

index 3727bf0677cdcef7afcb9c0e448a5e083e07ec77..a913d27389ab592e8f0b9acfc1758e8d13931f20 100644 (file)
@@ -1477,6 +1477,8 @@ class QueueListener(object):
             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:
index d12e1e57455d6fa4d529507d47c9fe8671af67ed..97c13a4c521483489650ff1794413a56a61d9e98 100644 (file)
@@ -3495,6 +3495,16 @@ if hasattr(logging.handlers, 'QueueListener'):
                                     [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)
 
diff --git a/Misc/NEWS.d/next/Library/2019-05-06-18-28-38.bpo-36813.NXD0KZ.rst b/Misc/NEWS.d/next/Library/2019-05-06-18-28-38.bpo-36813.NXD0KZ.rst
new file mode 100644 (file)
index 0000000..e89358a
--- /dev/null
@@ -0,0 +1,2 @@
+Fix :class:`~logging.handlers.QueueListener` to call ``queue.task_done()``
+upon stopping. Patch by Bar Harel.