Ned Batchelder: Bug #915: solved!
DRANK

Monday 13 January 2020Yesterday I pleaded, Bug #915: please help! It got posted to Hacker News, where Robert Xiao (nneonneo) did some impressive debugging and found the answer.The user’s code used mocks to simulate an OSError when trying to make temporary files (source):with patch('tempfile._TemporaryFileWrapper') as mock_ntf:mock_ntf.side_effect = OSError()Inside tempfile.NamedTemporaryFile, the error handling misses the possibility that _TemporaryFileWrapper will fail (source):(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type)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)raiseIf _TemporaryFileWrapper fails, the file descriptor fd is closed, but the file object referencing it still exists. Eventually, it will be garbage collected, and the file descriptor it references will be closed again.But file descriptors are ju…

nedbatchelder.com
Related Topics: