[New-bugs-announce] [issue16477] tarfile fails to close file handles in case of exception

Sam Thursfield report at bugs.python.org
Thu Nov 15 13:49:47 CET 2012


New submission from Sam Thursfield:

Exceptions such as disk full during extraction cause tarfile to leak file handles. Besides being messy, it causes real problems if, for example, the target file is on a mount that should be unmounted before the program exits - in this case, the unmount will fail as there are still open file handles.

Simplest solution I can see is to change:

    def makefile(self, tarinfo, targetpath):
        """Make a file called targetpath.
        """
        source = self.extractfile(tarinfo)
        target = bltn_open(targetpath, "wb")
        copyfileobj(source, target)
        source.close()
        target.close()

to this:

    def makefile(self, tarinfo, targetpath):
        """Make a file called targetpath.
        """
        source = self.extractfile(tarinfo)
        try:
            with open(targetpath, "wb") as target:
                shutil.copyfileobj(source, target)
        finally:
            source.close()

----------
components: Library (Lib)
messages: 175616
nosy: ssam
priority: normal
severity: normal
status: open
title: tarfile fails to close file handles in case of exception
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16477>
_______________________________________


More information about the New-bugs-announce mailing list