[issue19523] logging.FileHandler - using of delay argument case handler leaks

2013-11-07 Thread DDGG

New submission from DDGG:

Issue File: Python26\Lib\logging\__init__.py

class FileHandler(StreamHandler):
"""
A handler class which writes formatted logging records to disk files.
"""
def __init__(self, filename, mode='a', encoding=None, delay=0):
"""
Open the specified file and use it as the stream for logging.
"""
#keep the absolute path, otherwise derived classes which use this
#may come a cropper when the current directory changes
if codecs is None:
encoding = None
self.baseFilename = os.path.abspath(filename)
self.mode = mode
self.encoding = encoding
if delay:
#We don't open the stream, but we still need to call the
#Handler constructor to set level, formatter, lock etc.
Handler.__init__(self)   ## 1. here will insert instance into 
logging._handlerList
self.stream = None
else:
StreamHandler.__init__(self, self._open())

def close(self):
"""
Closes the stream.
"""
if self.stream:   ## 2. when delay=1, here should call 
Handler.close(self), or the instance still store in logging._handlerList, lead 
to leak (instance's owner's __del__ will not be called).
self.flush()
if hasattr(self.stream, "close"):
self.stream.close()
StreamHandler.close(self)
self.stream = None


leak demo:

import logging
import time

def using_handler():
filename = "test.log"
handler = logging.FileHandler(filename, mode="w", delay=1)
handler.close()

def test():
while True:
using_handler()
time.sleep(.01)

if __name__ == "__main__":
test()


If you run this script, and then view the Task Manager for this process's 
handlers, it is growing forever.


Solution: very easy

Fix the method FileHandler.close:

def close(self):
"""
Closes the stream.
"""
if self.stream:
self.flush()
if hasattr(self.stream, "close"):
self.stream.close()
StreamHandler.close(self)
self.stream = None
else:   
Handler.close(self) 


regards
DDGG

--
components: Library (Lib)
messages: 202403
nosy: DDGG
priority: normal
severity: normal
status: open
title: logging.FileHandler - using of delay argument case handler leaks
type: behavior
versions: Python 2.6

___
Python tracker 
<http://bugs.python.org/issue19523>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue19523] logging.FileHandler - using of delay argument case handle leaks

2013-11-07 Thread DDGG

Changes by DDGG :


--
title: logging.FileHandler - using of delay argument case handler leaks -> 
logging.FileHandler - using of delay argument case handle leaks

___
Python tracker 
<http://bugs.python.org/issue19523>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue19523] logging.FileHandler - using of delay argument case handle leaks

2013-11-07 Thread DDGG

DDGG added the comment:

If you run this script, and then view the Task Manager for this process's 
handles, it is growing forever.

handlers -> handles. this leak handle is Handler.lock object, because the 
instance was not be garbage-collected.

--

___
Python tracker 
<http://bugs.python.org/issue19523>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com