[issue41483] Do not acquire lock in MemoryHandler.flush() if no target defined
Irit Katriel
report at bugs.python.org
Fri Aug 7 07:57:16 EDT 2020
Irit Katriel <iritkatriel at yahoo.com> added the comment:
Actually the lock is needed and it is currently missing from setTarget.
The script below causes handle to be called on 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")
print("%s" % target.stream.getvalue().splitlines())
------------------------------------------------
----------
nosy: +iritkatriel
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41483>
_______________________________________
More information about the Python-bugs-list
mailing list