plone / plone.restapi

RESTful API for Plone.
http://plonerestapi.readthedocs.org/
86 stars 78 forks source link

ResourceWarning: unclosed file #836

Closed tisto closed 2 years ago

tisto commented 4 years ago

When running jMeter performance tests on Python 3 I ran into lots of those tracebacks:

/Users/timo/.buildout/eggs/ZODB-5.5.1-py3.7.egg/ZODB/blob.py:339: ResourceWarning: unclosed file <_io.FileIO name='/var/folders/_0/57n3vm9j04s63yq3twfxx9v00000gn/T/BUCvncbbzzi' mode='rb' closefd=True>
  super(BlobFile, self).close()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

I guess we need to properly close a file that we open. Python 3 seems to be more picky about this...

tisto commented 4 years ago

I was able to track this down to a plone.scale call:

    1/1 (100.0%)> /Users/timo/workspace/plone/plone.restapi/src/plone/restapi/imaging.py(57)get_original_image_url()
-> scale = images_view.scale(
(Pdb) images_view.scale(fieldname, width=width, height=height, direction="thumbnail")
/Users/timo/.buildout/eggs/ZODB-5.5.1-py3.7.egg/ZODB/blob.py:339: ResourceWarning: unclosed file <_io.FileIO name='/var/folders/zw/wqv1f2gd4zdc43t1j834x5580000gn/T/BUC60nli4o_' mode='rb' closefd=True>
  super(BlobFile, self).close()
ResourceWarning: Enable tracemalloc to get the object allocation traceback
<plone.namedfile.scaling.ImageScale object at 0x1198dbf90>

Just a guess about what could be causing this:

https://github.com/plone/plone.scale/blob/b442814a8cd9b2c4c16057459d09813d861b9e77/plone/scale/scale.py#L57

tisto commented 4 years ago

I created a first fix:

https://github.com/plone/plone.namedfile/pull/88

Will continue to look into it tomorrow...