Skip to content

TST: --open-files did not catch ResourceWarning #7913

Closed

Description

@pllim

Is this is not really an issue, feel free to close, as I am not familiar with the internals of open files check. Example log: https://travis-ci.org/astropy/astropy/jobs/442639768

It seems weird to me that --open-files option still reported passing despite the presence of following warning:

sys:1: ResourceWarning: unclosed file <_io.BufferedWriter name=13>

cc @drdavella

Activity

  1. astrofrog commented on Oct 18, 2018

    @astrofrog
    Member
  2. pllim commented on Oct 18, 2018

    @pllim
    MemberAuthor

    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.)

  3. pllim commented on Oct 18, 2018

    @pllim
    MemberAuthor
  4. drdavella commented on Oct 19, 2018

    @drdavella
    Contributor

    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 😝?)

    def test_file_exists(self, capsys):
    fitsheader.main([self.data('arange.fits')])
    out, err = capsys.readouterr()
    assert out.splitlines()[1].startswith(
    'SIMPLE = T / conforms to FITS standard')
    assert err == ''

    It's also entirely possible that this is just a red herring.

  5. drdavella commented on Oct 19, 2018

    @drdavella
    Contributor

    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?

  6. drdavella commented on Oct 20, 2018

    @drdavella
    Contributor

    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:

    #!/usr/bin/env python -W error::ResourceWarning
    
    import psutil
    from astropy.io.fits.scripts import fitsheader
    
    p = psutil.Process()
    
    print('before', p.open_files())
    fitsheader.main(['astropy/io/fits/tests/data/arange.fits'])
    print('after', p.open_files())

    And here's the output:

    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.

  7. reopened this on Oct 21, 2018
  8. pllim commented on Oct 21, 2018

    @pllim
    MemberAuthor

    Thanks for the detail investigation! I have re-opened this issue.

  9. pllim commented on Oct 26, 2018

    @pllim
    MemberAuthor

    While looking into #7928, I might have found one of the cause:

    astropy/coordinates/tests/test_solar_system.py::TestPositionsGeocentric::()::test_de432s_planet[mercury]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionsGeocentric::()::test_de432s_planet[jupiter]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionsGeocentric::()::test_de432s_planet[sun]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionsGeocentric::()::test_de432s_moon
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionKittPeak::()::test_de432s_planet[mercury]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionKittPeak::()::test_de432s_planet[jupiter]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionKittPeak::()::test_de432s_moon
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionKittPeak::()::test_custom_kernel_spec_body[mercury]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::TestPositionKittPeak::()::test_custom_kernel_spec_body[jupiter]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::test_get_sun_consistency[time0]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::test_get_sun_consistency[time1]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::test_get_sun_consistency[time2]
      .../astropy/coordinates/solar_system.py:364: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        get_velocity=False)
    
    astropy/coordinates/tests/test_solar_system.py::test_barycentric_velocity_consistency[mercury-pos_tol0-vel_tol0]
      .../astropy/coordinates/solar_system.py:330: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        return _get_body_barycentric_posvel(body, time, ephemeris)
    
    astropy/coordinates/tests/test_solar_system.py::test_barycentric_velocity_consistency[jupiter-pos_tol1-vel_tol1]
      .../astropy/coordinates/solar_system.py:330: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        return _get_body_barycentric_posvel(body, time, ephemeris)
    
    astropy/coordinates/tests/test_solar_system.py::test_barycentric_velocity_consistency[earth-pos_tol2-vel_tol2]
      .../astropy/coordinates/solar_system.py:330: ResourceWarning: unclosed file <_io.BufferedReader name='...cache/astropy/download/py3/d66c2153188958472fec67016b4b4e7a'>
        return _get_body_barycentric_posvel(body, time, ephemeris)
    

    @eteq or @adrn , any suggestion on how to fix?

  10. pllim commented on Oct 27, 2018

    @pllim
    MemberAuthor

    Wait, maybe this particular one reported originally is from io.fits...

  11. pllim commented on Oct 31, 2018

    @pllim
    MemberAuthor

    After looking at the FITS tests, which produced hundreds of such warnings, I am not sure if we want to catch 'em all.

  12. drdavella commented on Oct 31, 2018

    @drdavella
    Contributor

    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.

  13. saimn commented on Nov 13, 2018

    @saimn
    Contributor

    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 !

  14. jdavies-st commented on Nov 21, 2018

    @jdavies-st
    Contributor

    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.

  15. bsipocz commented on Feb 21, 2019

    @bsipocz
    Member

    addressed by #8439

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions