-
Notifications
You must be signed in to change notification settings - Fork 887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ResourceWarning: unclosed file with request.POST['file'].file #3599
Comments
Opening a file without closing it... yep that'll trigger. @view_config(route_name='file_upload', renderer='json')
def map_upload(request):
with request.POST['file'].file as fp:
pass
with open('/some/path/on/local/disk/some.json') as infile:
infile.read()
return {} |
I opened this issue on webob which could help in the future but the behavior you're seeing is expected right now (unfortunately). |
Sorry that return was obviously a mistake in my pasted snippet. The problem is the same when return is outside the with block. |
The issue issue isn't the return, it's the @mmerickel sample puts that in a |
I tried it with @mmerickel's with statement, it didn't help either. It took a lot of tries, like 6-8 but it triggered the warning:
|
Sorry the context manager is actually on the @view_config(route_name='file_upload', renderer='json')
def map_upload(request):
with request.POST['file'] as field:
data = field.value
with open('/some/path/on/local/disk/some.json') as infile:
infile.read()
return {} If you want to consume the file directly without loading it into a full memory buffer then you can read |
The following still doesn't work (actually this was what I tried as a very first step):
|
Apologies I’ll have to try to find a repro to offer more help. Closing the field file is usually enough. |
Closing the It looks like your WSGI server does not already give Pyramid a Edit: Actually WebOb doesn't check if the provided |
Ah I missed that it was due to the tempfile created from |
This is in local development setup with Waitress. |
@hyperknot and waitress does, but WebOb doesn't adequately check for it. See the edit above by @archoversight. |
Moving this issue to Pylons/webob#416. |
Describe the bug
When I POST to the following endpoint snippet, after a few tries I get a ResourceWarning: unclosed file.
Output with
PYTHONTRACEMALLOC=25
Output 1:
In a less minimal form (closer to real world logic), the output was:
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Nothing like this should happen.
Additional context
This is really strange and it took me a while to reproduce this minimal case, it honestly makes no sense to me. But it's 100% reproducible, so I'm sharing it here as it is. The file I'm uploading is 2.8 MB in size.
Python 3.7 / macOS
pyramid==1.10.4
WebOb==1.8.6
waitress==1.4.4
The text was updated successfully, but these errors were encountered: