Skip to content
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 after uploading large files #1242

Closed
perrinjerome opened this issue Jan 4, 2025 · 0 comments · Fixed by #1243
Closed

ResourceWarning after uploading large files #1242

perrinjerome opened this issue Jan 4, 2025 · 0 comments · Fixed by #1243

Comments

@perrinjerome
Copy link
Contributor

What I did:

Upload a file with an html like this that one:

<html>
    <form action="/" method="post" enctype="multipart/form-data">
        <input type="file" id="f" name="f">
        <button type="submit">Upload</button>
    </form>
</html>

What I expect to happen:

no warning

What actually happened:

warning on the console:

./Zope/src/ZPublisher/HTTPRequest.py:195: ResourceWarning: unclosed file <_io.BufferedRandom name=18>
  self.form.clear()
ResourceWarning: Enable tracemalloc to get the object allocation traceback

Very small files do not cause this warning, because a BytesIO is used

What version of Python and Zope/Addons I am using:

This happens on current Zope master branch.

Suggested fix

Multipart documentation recommends closing MultipartPart at the end of request, quoting https://multipart.readthedocs.io/en/latest/usage.html#streaming-parser

Do not forget to close all parts after use to free up resources and avoid ResourceWarnings. Framework developers may want to add logic that automatically frees up resources after the request ended.

Zope request does not keep a reference to the MultipartPart, but creates a FormField

field = FormField(
name=part.name,
file=part.file,
filename=part.filename,
headers=part.headers)

which later becomes a FileUpload

if hasattr(item, 'file') and \
hasattr(item, 'filename') and \
hasattr(item, 'headers'):
item = FileUpload(item, self.charset)

so calling MultipartPart.close is not directly applicable, but since what this method does ( code for reference ) is just calling close on the underlying file, calling close on the FileUpload is equivalent.

The fix I'm suggesting is to call close on all FileUploads during HTTPRequest.clear ( I'm sending a pull request doing this ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant