Skip to content

Commit

Permalink
HTTPRequest: close file uploads to prevent ResourceWarning (#1243)
Browse files Browse the repository at this point in the history
* testHTTPRequest: use larger file for test_processInputs_w_large_input_gets_tempfile

This test was not using a large enough file to use temporary file, but
the test was not really asserting that a temporary file was used.

* HTTPRequest: close file uploads to prevent ResourceWarning
  • Loading branch information
perrinjerome authored Jan 7, 2025
1 parent 7802542 commit 8c140a3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
5.11.2 (unreleased)
-------------------

- Fix a ``ResourceWarning`` emitted when uploading large files.
(`#1242 <https://github.com/zopefoundation/Zope/issues/1242>`_)

- OFS/cachable: fix *Cache this object using* label in ZMI.

- Include versions constraints for production and non-production dependencies
Expand Down
3 changes: 3 additions & 0 deletions src/ZPublisher/HTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ def clear(self):
self.stdin = None
self._file = None
self._fs = None
for f in self.form.values():
if isinstance(f, FileUpload):
f.close()
self.form.clear()
# we want to clear the lazy dict here because BaseRequests don't have
# one. Without this, there's the possibility of memory leaking
Expand Down
8 changes: 5 additions & 3 deletions src/ZPublisher/tests/testHTTPRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,10 @@ def test_processInputs_w_large_input_gets_tempfile(self):
req.processInputs()
f = req.form.get('largefile')
self.assertTrue(f.name)
self.assertEqual(4006, len(f.file.read()))
f.file.close()
self.assertEqual(40006, len(f.file.read()))
self.assertTrue(f.file.fileno())
req.clear()
self.assertTrue(f.file.closed)

def test_processInputs_with_file_upload_gets_iterator(self):
# checks fileupload object supports the iterator protocol
Expand Down Expand Up @@ -1640,7 +1642,7 @@ def __init__(self, file):
test %s
--12345--
''' % (b'test' * 1000)
''' % (b'test' * 10000)

TEST_ISSUE_1095_DATA = b'''
--12345
Expand Down

0 comments on commit 8c140a3

Please sign in to comment.