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 7 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
Fix test mypy issues
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
  • Loading branch information
HonakerM committed Jul 11, 2025
commit 297c09e26edf58d5ce64903c7d08ed3d3c5027b2
12 changes: 7 additions & 5 deletions tests/test_formparsers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import os
import threading
from contextlib import AbstractContextManager, nullcontext as does_not_raise
from io import BytesIO
from pathlib import Path
from tempfile import SpooledTemporaryFile
import threading
from typing import Any
from unittest import mock

Expand Down Expand Up @@ -321,21 +321,23 @@ def test_multipart_request_mixed_files_and_data(tmpdir: Path, test_client_factor
}


class ThreadTrackingSpooledTemporaryFile(SpooledTemporaryFile):
class ThreadTrackingSpooledTemporaryFile(SpooledTemporaryFile[bytes]):
"""Helper class to track which threads performed the rollover operation. This is
not threadsafe/multi-test safe"""

rollover_threads: set[int] = set()
rollover_threads: set[int | None] = set()

def rollover(self):
def rollover(self) -> None:
ThreadTrackingSpooledTemporaryFile.rollover_threads.add(threading.current_thread().ident)
return super().rollover()
super().rollover()


def test_multipart_request_large_file(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
"""Test that Spooled file rollovers happen in background threads"""
data = BytesIO(b" " * MultiPartParser.spool_max_size * 2)

# Mock the formparser to use our monitoring class
ThreadTrackingSpooledTemporaryFile.rollover_threads.clear()
with mock.patch("starlette.formparsers.SpooledTemporaryFile", ThreadTrackingSpooledTemporaryFile):
client = test_client_factory(app_monitor_thread)
response = client.post(
Expand Down