This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging.FileHandler - using of delay argument case handle leaks
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: DDGG, python-dev, vinay.sajip
Priority: normal Keywords:

Created on 2013-11-08 03:40 by DDGG, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg202403 - (view) Author: DDGG (DDGG) Date: 2013-11-08 03:40
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
msg202406 - (view) Author: DDGG (DDGG) Date: 2013-11-08 05:39
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.
msg202969 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-11-15 20:43
New changeset bbb227b96c45 by Vinay Sajip in branch '2.7':
Issue #19523: Closed FileHandler leak which occurred when delay was set.
http://hg.python.org/cpython/rev/bbb227b96c45

New changeset 058810fe1b98 by Vinay Sajip in branch '3.3':
Issue #19523: Closed FileHandler leak which occurred when delay was set.
http://hg.python.org/cpython/rev/058810fe1b98

New changeset a3640822c3e6 by Vinay Sajip in branch 'default':
Closes #19523: Merged fix from 3.3.
http://hg.python.org/cpython/rev/a3640822c3e6
History
Date User Action Args
2022-04-11 14:57:53adminsetgithub: 63722
2013-11-15 20:43:01python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg202969

resolution: fixed
stage: resolved
2013-11-08 21:23:22vinay.sajipsetnosy: + vinay.sajip
2013-11-08 05:39:26DDGGsetmessages: + msg202406
2013-11-08 05:35:22DDGGsettitle: logging.FileHandler - using of delay argument case handler leaks -> logging.FileHandler - using of delay argument case handle leaks
2013-11-08 03:40:03DDGGcreate