Skip to content

Make UploadFile check for future rollover #2962

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Slightly improve efficency of Size Check
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
  • Loading branch information
HonakerM committed Jul 10, 2025
commit f14ee57f3ad4659a0496e509f9fa9ef08baf5697
8 changes: 6 additions & 2 deletions starlette/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ def __init__(
self.size = size
self.headers = headers or Headers()

# Capture max size from SpooledTemporaryFile if one is provided. This
# slightly speeds up future checks. Note 0 means unlimited mirroring
# SpooledTemporaryFile's __init__
self._max_mem_size = getattr(self.file, "_max_size", 0)

@property
def content_type(self) -> str | None:
return self.headers.get("content-type", None)
Expand All @@ -444,9 +449,8 @@ def _will_roll(self, size_to_add: int) -> bool:
return True

# Check for SpooledTemporaryFile._max_size
max_size = getattr(self.file, "_max_size", None)
future_size = self.file.tell() + size_to_add
return bool(future_size > max_size) if max_size is not None else False
return bool(future_size > self._max_mem_size) if self._max_mem_size else False

async def write(self, data: bytes) -> None:
new_data_len = len(data)
Expand Down