[New-bugs-announce] [issue4265] shutil.copyfile() leaks file descriptors when disk fills

Ammon Riley report at bugs.python.org
Wed Nov 5 21:26:18 CET 2008


New submission from Ammon Riley <ammon.riley at gmail.com>:

If the disk fills up during the copy operation, shutil.copyfile() leaks
file descriptors. 

The problem is the order of the close() statements in the finally block.
In the event the copy operation runs out of disk space, the fdst.close()
call triggers an IOError, which prevents the fsrc.close() call from being
called. 

Swapping the two calls, so that fsrc is closed first prevents this issue,
though it doesn't solve the underlying problem that IOErrors raised during
the close() operation will prevent the second close() from being called.

A probably better solution:

    def copyfile(src, dst):
        """Copy data from src to dst"""
        if _samefile(src, dst):
            raise Error, "`%s` and `%s` are the same file" % (src, dst)

        fsrc = None
        fdst = None
        try:
            fsrc = open(src, 'rb')
            fdst = open(dst, 'wb')
            copyfileobj(fsrc, fdst)
        finally:
            try:
                if fsrc:
                    fsrc.close()
            finally:
                if fdst:
                    fdst.close()

----------
components: Library (Lib)
messages: 75530
nosy: ammon_riley
severity: normal
status: open
title: shutil.copyfile() leaks file descriptors when disk fills
type: resource usage
versions: Python 2.5

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


More information about the New-bugs-announce mailing list