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.

Author nneonneo
Recipients nneonneo
Date 2020-01-13.04:56:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1578891407.19.0.452054572225.issue39318@roundup.psfhosted.org>
In-reply-to
Content
tempfile.NamedTemporaryFile creates its wrapper like so:

    try:
        file = _io.open(fd, mode, buffering=buffering,
                        newline=newline, encoding=encoding, errors=errors)

        return _TemporaryFileWrapper(file, name, delete)
    except BaseException:
        _os.unlink(name)
        _os.close(fd)
        raise

If _TemporaryFileWrapper throws any kind of exception (even KeyboardInterrupt), this closes `fd` but leaks a valid `file` pointing to that fd. The `file` will later attempt to close the `fd` when it is collected, which can lead to subtle bugs. (This particular issue contributed to this bug: https://nedbatchelder.com/blog/202001/bug_915_please_help.html)

This should probably be rewritten as:

    try:
        file = _io.open(fd, mode, buffering=buffering,
                        newline=newline, encoding=encoding, errors=errors)
    except:
        _os.unlink(name)
        _os.close(fd)
        raise

    try:
        return _TemporaryFileWrapper(file, name, delete)
    except BaseException:
        _os.unlink(name)
        file.close()
        raise

or perhaps use nested try blocks to avoid the _os.unlink duplication.
History
Date User Action Args
2020-01-13 04:56:47nneonneosetrecipients: + nneonneo
2020-01-13 04:56:47nneonneosetmessageid: <1578891407.19.0.452054572225.issue39318@roundup.psfhosted.org>
2020-01-13 04:56:47nneonneolinkissue39318 messages
2020-01-13 04:56:46nneonneocreate