Skip to content

Commit c2e2878

Browse files
bysiberKludex
andauthored
Move parser.finalize() inside try/except in MultiPartParser.parse() (#3153)
parser.finalize() can trigger callbacks that raise MultiPartException (e.g. when multipart data is truncated or malformed). Since it was called outside the try/except block, any exception raised during finalize would skip the file cleanup, leaking SpooledTemporaryFile handles until the garbage collector eventually collects them. Moving finalize() inside the try block ensures that temporary files are properly closed on any MultiPartException, whether it occurs during chunk processing or during finalization. Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
1 parent 89630a8 commit c2e2878

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

starlette/formparsers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ async def parse(self) -> FormData:
266266
await part.file.seek(0)
267267
self._file_parts_to_write.clear()
268268
self._file_parts_to_finish.clear()
269+
parser.finalize()
269270
except MultiPartException as exc:
270271
# Close all the files if there was an error.
271272
for file in self._files_to_close_on_error:
272273
file.close()
273274
raise exc
274275

275-
parser.finalize()
276276
return FormData(self.items)

0 commit comments

Comments
 (0)