You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TST: --open-files did not catch ResourceWarning #7913
I have some reason to believe this warning is being caused by this particular test, although I'm not sure why, and it doesn't really help much yet. (Is anyone surprised that it comes from io.fits 😝?)
So the ResourceWarning is definitely being caused by an oversight in astropy/io/fits/scripts/fitsheader.py. I can make it go away by adding a close() in the right place.
The real question still is why isn't getting picked up by pytest-openfiles?
I think this is not a problem with pytest-openfiles, but there's something really strange going on, and I'm running out of ideas. I think this distills the problem nicely:
before []
# HDU 0 in astropy/io/fits/tests/data/arange.fits:
SIMPLE = T / conforms to FITS standard
BITPIX = 32 / array data type
NAXIS = 3 / number of array dimensions
NAXIS1 = 11
NAXIS2 = 10
NAXIS3 = 7
EXTEND = T
Exception ignored in: <_io.FileIO name='astropy/io/fits/tests/data/arange.fits' mode='rb' closefd=True>
ResourceWarning: unclosed file <_io.FileIO name='astropy/io/fits/tests/data/arange.fits' mode='rb' closefd=True>
after []
The key takeaway is that even though we get a resource warning for the file being open at the end of the process, it does not show up in the list of open files for this process using psutil (which, incidentally, is the what pytest-openfiles uses to detect open files).
I guess it's possible that the file handle is owned by another process (???) and so looking at the open files for just this process is not sufficient. However, assuming this is actually the problem (and I really don't know), I'm not sure we can handle this in a systematic way in pytest-openfiles (due to permissions issues, etc.).
It's fairly easy to fix this particular issue in fitsheader. But I'm not sure how we could have detected it automatically.
EDIT: Actually, after all of that, I guess the obvious thing would be to make pytest-openfiles look for ResourceWarnings and report them as errors.
IIRC, I think the problem is that many of them come from other libraries. If that's the case, we would want to somehow filter to get only the ones that come from astropy, but I'm not sure there's a straightforward way to do that in Python.
That's one of the reasons I haven't really made any progress on implementing this functionality in pytest-openfiles, although it would still be really useful.
Re #7913 (comment) above, when you get a ResourceWarning for an open file from python, it's because the garbage collector detected the unclosed file, closed it and printed that warning, so it is not surprising that p.open_files() is empty before and after.
Activity
astrofrog commentedon Oct 18, 2018
Maybe this issue should be at https://github.com/astropy/pytest-openfiles?
pllim commentedon Oct 18, 2018
After some digging... could this be #6942 ? (Or maybe not since I don't see this warning in the log linked to that other issue.)
pllim commentedon Oct 18, 2018
Moved to astropy/pytest-openfiles#14
drdavella commentedon Oct 19, 2018
I have some reason to believe this warning is being caused by this particular test, although I'm not sure why, and it doesn't really help much yet. (Is anyone surprised that it comes from
io.fits😝?)astropy/astropy/io/fits/tests/test_fitsheader.py
Lines 16 to 21 in 66c7967
It's also entirely possible that this is just a red herring.
drdavella commentedon Oct 19, 2018
So the
ResourceWarningis definitely being caused by an oversight inastropy/io/fits/scripts/fitsheader.py. I can make it go away by adding aclose()in the right place.The real question still is why isn't getting picked up by
pytest-openfiles?drdavella commentedon Oct 20, 2018
I think this is not a problem with
pytest-openfiles, but there's something really strange going on, and I'm running out of ideas. I think this distills the problem nicely:And here's the output:
The key takeaway is that even though we get a resource warning for the file being open at the end of the process, it does not show up in the list of open files for this process using
psutil(which, incidentally, is the whatpytest-openfilesuses to detect open files).I guess it's possible that the file handle is owned by another process (???) and so looking at the open files for just this process is not sufficient. However, assuming this is actually the problem (and I really don't know), I'm not sure we can handle this in a systematic way in
pytest-openfiles(due to permissions issues, etc.).It's fairly easy to fix this particular issue in
fitsheader. But I'm not sure how we could have detected it automatically.EDIT: Actually, after all of that, I guess the obvious thing would be to make
pytest-openfileslook forResourceWarningsand report them as errors.pllim commentedon Oct 21, 2018
Thanks for the detail investigation! I have re-opened this issue.
pllim commentedon Oct 26, 2018
While looking into #7928, I might have found one of the cause:
@eteq or @adrn , any suggestion on how to fix?
pllim commentedon Oct 27, 2018
Wait, maybe this particular one reported originally is from
io.fits...pllim commentedon Oct 31, 2018
After looking at the FITS tests, which produced hundreds of such warnings, I am not sure if we want to catch 'em all.
drdavella commentedon Oct 31, 2018
IIRC, I think the problem is that many of them come from other libraries. If that's the case, we would want to somehow filter to get only the ones that come from
astropy, but I'm not sure there's a straightforward way to do that in Python.That's one of the reasons I haven't really made any progress on implementing this functionality in
pytest-openfiles, although it would still be really useful.saimn commentedon Nov 13, 2018
Actually there is a way to show a traceback for ResourceWarning, with tracemalloc:
python3 -Wd -X tracemalloc=5. From https://pythondev.readthedocs.io/debug_tools.html#resourcewarning, and thanks to @vstinner !jdavies-st commentedon Nov 21, 2018
Re #7913 (comment) above, when you get a
ResourceWarningfor an open file from python, it's because the garbage collector detected the unclosed file, closed it and printed that warning, so it is not surprising thatp.open_files()is empty before and after.bsipocz commentedon Feb 21, 2019
addressed by #8439