[New-bugs-announce] [issue11787] File handle leak in TarFile lib

Pulin Shah report at bugs.python.org
Wed Apr 6 20:42:55 CEST 2011


New submission from Pulin Shah <shahpr at gmail.com>:

I ran into a problem the other day while trying to extract a slightly corrupted tar file.  I suspect this problem is really only an issue on Windows systems.  I am running Python 2.7.1 r271:86832 win32.

The following code (simplified) snipet

try:
	tar = tarfile.open(args.file)
	tar.extractall(basefolder)
	tar.close()
except tarfile.ReadError:
	shutil.rmtree(basefolder)
except IOError:
	shutil.rmtree(basefolder)

was throwing a WindowsError on the rmtree calls.

This is due to the tarfile library not closing file handles in the case of an exception in the copyfileobj function, and Windows inability to delete open files.  

I was able to patch the issue locally by modifying tarfile's makefile function as follows:

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

There is probably a cleaner way of implementing it.

I'm hoping you can integrate this patch into later versions of the lib.

Thanks.

----------
components: Library (Lib)
messages: 133153
nosy: shahpr
priority: normal
severity: normal
status: open
title: File handle leak in TarFile lib
type: behavior
versions: Python 2.7

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


More information about the New-bugs-announce mailing list