Closed mcr-ksh closed 2 years ago
At some point you probably access request.body
. And if the body is too big it puts it into a temporary file (opens a file). From what I can see you're responsible for closing the file, e.g.:
with request.body as body:
# process body
# here body is closed
Unless you do, you get the warning. Such warnings are displayed only in debug mode. To get more lines of traceback, run it with PYTHONTRACEMALLOC=N
, where N
is the desired number of lines of the traceback.
Similar issue: #665.
Turns out using NamedTemporaryFile
instead of TemporaryFile
fixes the ResourceWarning
in most cases because NamedTemporaryFile
(indirectly) implements __del__
to explicitly close (and remove) the file. This is not needed for TemporaryFile
because these are unlinked immediately, which is why these are still technically 'open' when the garbage collector collects the file, which in turn triggers the warning.
Only downside is that on a hard crash, a temporary file may remain on disk, so this is not an ideal solution. Deprecating a request.body
that lives longer than the request and switching back to TemporaryFile
might be the cleaner option. Until then, I'll consider this fixed (in master). I'll keep one of the related issues open.
I know that this is old, but for anyone who stumbles upon this: even if you're not directly accessing request.body
but rather referencing your uploaded file with request.files.get("NAME").file
, you still need to close the body with request.body.close()
, otherwise you'll also get this warning.
There is a resource leek that I cannot determine from my code. I guess it's from bottle.
On Fileupload (via tracemalloc):
Function:
Command: curl -X POST -F 'file=@/tmp/pic.jpg' http://irsdev:5000/api/0.5