[New-bugs-announce] [issue46755] QueueHandler logs stack_info twice

Erik Montnemery report at bugs.python.org
Tue Feb 15 03:36:55 EST 2022

New submission from Erik Montnemery <erik.montnemery at gmail.com>:

logging.handlers.QueueHandler logs stack twice when stack_info=True:

>>> import logging
>>> from logging.handlers import QueueHandler, QueueListener
>>> from queue import Queue
>>> q = Queue()
>>> logging.getLogger().addHandler(QueueHandler(q))
>>> listener = QueueListener(q, logging.StreamHandler())
>>> listener.start()
>>> _LOGGER.error("Hello", stack_info=True)
Stack (most recent call last):
  File "<stdin>", line 1, in <module>
Stack (most recent call last):
  File "<stdin>", line 1, in <module>

Reproduced on CPython 3.9.9, but the code is unchanged in 3.10 and 3.11, so the issue should exist there too.

Patching QueueHandler.prepare() to set stack_info to None seems to fix this:

diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py
index d42c48de5f..7cd5646d85 100644
--- a/Lib/logging/handlers.py
+++ b/Lib/logging/handlers.py
@@ -1452,6 +1452,7 @@ def prepare(self, record):
         record.args = None
         record.exc_info = None
         record.exc_text = None
+        record.stack_info = None
         return record

     def emit(self, record):

Related issue: Issue34334, with patch https://github.com/python/cpython/pull/9537

components: Library (Lib)
messages: 413278
nosy: erik.montnemery
priority: normal
severity: normal
status: open
title: QueueHandler logs stack_info twice
type: behavior
versions: Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list