BUG: Fixed file handle leak in array_tofile.#17598
Conversation
b0c1eea to
6e301f6
Compare
cb60269 to
0a7e905
Compare
|
Thanks for the patch. I think the error handling is still generally wrong in this function. I think we need to aim for the C equivalent of: file = ...
try:
fd = npy_PyFile_Dup2(file)
try:
PyArray_ToFile(fd)
finally:
npy_PyFile_DupClose2(fd)
finally:
if own:
npy_PyFile_CloseFile(file)This would almost certainly be much easier to get right if the contents of the outer |
0a7e905 to
fae5759
Compare
numpy/core/tests/test_multiarray.py
Outdated
There was a problem hiding this comment.
Can you repeat this test with x.tofile(self.filename), to check it closes both the python handle and the fd?
There was a problem hiding this comment.
This second check is now added.
The dup-ed file handle created in array_tofile is now closed when PyArray_ToFile fails.
Co-authored-by: Eric Wieser <[email protected]>
db572dd to
e905185
Compare
If we pass a str to tofile() rather than an open file object, it will open a Python handle. Make sure that it gets closed correctly.
e905185 to
ef18ad1
Compare
|
Hi @eric-wieser, is there anything else to address for this PR? I believe I've implemented all of your review feedback. Thanks. |
eric-wieser
left a comment
There was a problem hiding this comment.
Sorry for forgetting about this - looks good, thanks!
|
Thanks @simon-graham |
Closes #17589
If the
PyArray_ToFilecall insidearray_tofilefails for any reason, we leak a dup-ed file handle. This causes problems especially on Windows OS, where any subsequent attempt to delete the file will fail because NumPy is still holding an open handle.This update ensures that we always close the handle.
The added test case forces
PyArray_ToFileto fail, which triggers the original problem.