Skip to content

Commit 5863cd7

Browse files
csm10495picnixz
andauthored
gh-132106: Ensure that running logging.handlers.QueueListener cannot be started again (GH-132444)
Prevents a thread leak Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 64b066a commit 5863cd7

File tree

5 files changed

+24
-0
lines changed

5 files changed

+24
-0
lines changed

Doc/library/logging.handlers.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,10 @@ possible, while any potentially slow operations (such as sending an email via
11861186
This starts up a background thread to monitor the queue for
11871187
LogRecords to process.
11881188

1189+
.. versionchanged:: next
1190+
Raises :exc:`RuntimeError` if called and the listener is already
1191+
running.
1192+
11891193
.. method:: stop()
11901194

11911195
Stops the listener.

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,10 @@ logging.handlers
819819
manager protocol, allowing it to be used in a :keyword:`with` statement.
820820
(Contributed by Charles Machalow in :gh:`132106`.)
821821

822+
* :meth:`QueueListener.start <logging.handlers.QueueListener.start>` now
823+
raises a :exc:`RuntimeError` if the listener is already started.
824+
(Contributed by Charles Machalow in :gh:`132106`.)
825+
822826

823827
mimetypes
824828
---------

Lib/logging/handlers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,9 @@ def start(self):
15611561
This starts up a background thread to monitor the queue for
15621562
LogRecords to process.
15631563
"""
1564+
if self._thread is not None:
1565+
raise RuntimeError("Listener already started")
1566+
15641567
self._thread = t = threading.Thread(target=self._monitor)
15651568
t.daemon = True
15661569
t.start()

Lib/test/test_logging.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,6 +4356,17 @@ def test_queue_listener_context_manager(self):
43564356
listener.stop()
43574357
self.assertIsNone(listener._thread)
43584358

4359+
def test_queue_listener_multi_start(self):
4360+
handler = TestHandler(support.Matcher())
4361+
with logging.handlers.QueueListener(self.queue, handler) as listener:
4362+
self.assertRaises(RuntimeError, listener.start)
4363+
4364+
with listener:
4365+
self.assertRaises(RuntimeError, listener.start)
4366+
4367+
listener.start()
4368+
listener.stop()
4369+
43594370
def test_queue_listener_with_StreamHandler(self):
43604371
# Test that traceback and stack-info only appends once (bpo-34334, bpo-46755).
43614372
listener = logging.handlers.QueueListener(self.queue, self.root_hdlr)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:meth:`QueueListener.start <logging.handlers.QueueListener.start>` now
2+
raises a :exc:`RuntimeError` if the listener is already started.

0 commit comments

Comments
 (0)