tracking down a test issue - ResourceWarning: unclosed file <_io.BufferedRandom

37 views
Skip to first unread message

Jonathan Vanasco

unread,
Feb 4, 2020, 3:52:57 PM2/4/20
to pylons-discuss
I keep encountering a particular `ResourceWarning: unclosed file <_io.BufferedRandom` in my test suite that I can not track down.  I'm hoping someone else may have an idea:

* In a suite of 10+ test classes, it only happens when 2 particular classes are run consecutively and in a particular order. It does not happen in the inverse.  
* this happens in a testapp setup, on the consecutive setup
* the error happens in this stack:

    pyramid.paster.get_appsettings("test.ini")
    ...
    plaster.loaders.find_loader()

It seems to trigger right after the final `return [EntryPointLoaderInfo...] 

The error is also coming from `packaging/version.py`

site-packages/pkg_resources/_vendor/packaging/version.py:141: ResourceWarning: unclosed file <_io.BufferedRandom name=5>


and is in the _cmpkey function:

    release = tuple(
        reversed(list(
            itertools.dropwhile(
                lambda x: x == 0,
                reversed(release),
            )
        ))
    )

I can't seem to recreate this, and I can't pinpoint things further to fix.  It happens somewhere in the pyramid/plaster/paste configuration.  

Does this situation look familiar to anyone?  This makes no sense.  I have a small hope that someone has experienced this before and knows where I should be focusing my efforts.

Jonathan Vanasco

unread,
Feb 4, 2020, 4:53:01 PM2/4/20
to pylons-discuss
After even more testing (half my day!)...

My app needed an `add_finished_callback` to close any `webob.compat.cgi_FieldStorage` objects in forms.

Michael Merickel

unread,
Feb 5, 2020, 1:15:23 PM2/5/20
to Pylons
If you're opening files you should use the context manager - for example:

with request.POST['file'] as field:
    data = field.file.read()

On Tue, Feb 4, 2020 at 3:53 PM Jonathan Vanasco <jona...@findmeon.com> wrote:
After even more testing (half my day!)...

My app needed an `add_finished_callback` to close any `webob.compat.cgi_FieldStorage` objects in forms.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/b87e949c-62f7-43b9-a609-d78bd8ea6015%40googlegroups.com.


--

Michael

Jonathan Vanasco

unread,
Feb 5, 2020, 2:39:24 PM2/5/20
to pylons-discuss

On Wednesday, February 5, 2020 at 1:15:23 PM UTC-5, Michael Merickel wrote:
If you're opening files you should use the context manager - for example:

with request.POST['file'] as field:
    data = field.file.read()

Thanks, Michael. My application code did that... but somewhere in the form management/validation stack, file objects were being opened and not closed.  I haven't tracked down exactly where that happens yet, so I went with the finished callback to close file-like objects.
Reply all
Reply to author
Forward
0 new messages