Skip to content

Close incomplete multipart upload files - #325

Open
Yeaseen wants to merge 1 commit into
Kludex:mainfrom
Yeaseen:fix-close-incomplete-upload
Open

Yeaseen wants to merge 1 commit into
Kludex:mainfrom
Yeaseen:fix-close-incomplete-upload

Conversation

@Yeaseen

@Yeaseen Yeaseen commented Sep 7, 2026

Copy link
Copy Markdown

Problem

When a multipart request ends before the current file part is completed,
FormParser.close() closes only the low-level parser. The active File or
decoder-backed writer remains open even though it was never delivered to the
application through on_file.

Completed files are different: once passed to on_file, their lifetime belongs
to the application and closing the parser must not close them.

Solution

Track whether the active multipart file has been transferred to on_file. On
FormParser.close(), close an active writer only when it is still owned by the
parser. Clear the writer after closing it so repeated calls to close() are
safe.

This closes both direct file writers and decoder-backed writers. It preserves
the configured UPLOAD_DELETE_TMP=False behavior: closing an unfinished file
releases its file descriptor but does not unlink the retained temporary file.

Tests

  • Verify incomplete binary, Base64, and quoted-printable uploads are closed
    exactly once.
  • Verify repeated FormParser.close() calls are safe.
  • Verify completed files delivered to on_file remain open and caller-owned.
  • Verify UPLOAD_DELETE_TMP=False continues to retain the file after closing.

Verification performed:

163 passed, 3 subtests passed
TOTAL 885 statements, 0 missing, 100% coverage
Ruff: passed
mypy: passed

Review in cubic

@codspeed

codspeed Bot commented Sep 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 5 untouched benchmarks
⏩ 5 skipped benchmarks1


Comparing Yeaseen:fix-close-incomplete-upload (ea974bf) with main (d9cb4c6)

Open in CodSpeed

Footnotes

  1. 5 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread python_multipart/multipart.py Outdated
Comment thread python_multipart/multipart.py Outdated
@Yeaseen
Yeaseen force-pushed the fix-close-incomplete-upload branch from 8cfa08f to b1c2e6c Compare September 7, 2026 07:37
@Yeaseen
Yeaseen force-pushed the fix-close-incomplete-upload branch from b1c2e6c to ea974bf Compare September 7, 2026 07:41
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 this pull request may close these issues.

1 participant