Mailing List Archive

[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler
New submission from Irit Katriel <iritkatriel@yahoo.com>:

The `logging.handlers.MemoryHandler.setTarget()` method does not acquire the lock, so it can change the target while flush is processing the buffer.

The script below causes flush to call target.handle when target is None, causing:

File "C:\Users\User\src\cpython\lib\logging\handlers.py", line 1265, in emit
self.flush()
File "C:\Users\User\src\cpython\lib\logging\handlers.py", line 1348, in flush
self.target.handle(record)
AttributeError: 'NoneType' object has no attribute 'handle'


------------------------------------------------
import io
import logging.handlers
import threading
import time

class SlowHandler:
def __init__(self):
self.stream = io.StringIO()
self.streamHandler = logging.StreamHandler(self.stream)

def handle(self, msg):
time.sleep(1)
self.streamHandler.handle(msg)

target = SlowHandler()
mem_hdlr = logging.handlers.MemoryHandler(10, logging.ERROR, target)
mem_logger = logging.getLogger('mem')
mem_logger.propagate = False
mem_logger.addHandler(mem_hdlr)

def toggleTarget():
time.sleep(1)
mem_hdlr.setTarget(None)

t = threading.Thread(target=toggleTarget, args=())
t.daemon = True
t.start()

while True:
time.sleep(0.1)
mem_logger.warning("warning not flushed")
mem_logger.error("error is flushed")

------------------------------------------------

----------
components: Library (Lib)
messages: 375001
nosy: iritkatriel
priority: normal
severity: normal
status: open
title: Race between setTarget and flush in logging.handlers.MemoryHandler
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
Change by Irit Katriel <iritkatriel@yahoo.com>:


----------
keywords: +patch
pull_requests: +20909
stage: -> patch review
pull_request: https://github.com/python/cpython/pull/21765

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
Change by Irit Katriel <iritkatriel@yahoo.com>:


----------
nosy: +vinay.sajip

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
Change by Irit Katriel <iritkatriel@yahoo.com>:


----------
versions: +Python 3.8, Python 3.9

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
Vinay Sajip <vinay_sajip@yahoo.co.uk> added the comment:


New changeset 2353d77fad7ed9d11d8a4d66b5dd1306cdb94125 by Irit Katriel in branch 'master':
bpo-41503: Fix race between setTarget and flush in logging.handlers.MemoryHandler (GH-21765)
https://github.com/python/cpython/commit/2353d77fad7ed9d11d8a4d66b5dd1306cdb94125


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>:


----------
nosy: +miss-islington
nosy_count: 2.0 -> 3.0
pull_requests: +21016
pull_request: https://github.com/python/cpython/pull/21897

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
Change by miss-islington <mariatta.wijaya+miss-islington@gmail.com>:


----------
pull_requests: +21017
pull_request: https://github.com/python/cpython/pull/21898

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment:


New changeset 2c050e52f1ccf5db03819e4ed70690521d67e9fa by Miss Islington (bot) in branch '3.9':
[3.9] bpo-41503: Fix race between setTarget and flush in logging.handlers.MemoryHandler (GH-21765) (GH-21897)
https://github.com/python/cpython/commit/2c050e52f1ccf5db03819e4ed70690521d67e9fa


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment:


New changeset 08f0a2109297e8a64e8636d47dce737e5b7ccf2c by Miss Islington (bot) in branch '3.8':
[3.8] bpo-41503: Fix race between setTarget and flush in logging.handlers.MemoryHandler (GH-21765) (GH-21898)
https://github.com/python/cpython/commit/08f0a2109297e8a64e8636d47dce737e5b7ccf2c


----------

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue41503] Race between setTarget and flush in logging.handlers.MemoryHandler [ In reply to ]
Change by Vinay Sajip <vinay_sajip@yahoo.co.uk>:


----------
resolution: -> fixed
stage: patch review -> resolved
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue41503>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com