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

Conversation

HonakerM
Copy link

@HonakerM HonakerM commented Jul 10, 2025

Summary

This PR ensures that during multi-part form parsing any slow disk IO happens in a background. This specifically fixes a case where a file would rollover during a write() call as talked about in this discussion.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

HonakerM added 4 commits July 10, 2025 10:16
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
@HonakerM HonakerM marked this pull request as ready for review July 10, 2025 14:48
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
Comment on lines 307 to 314
def test_multipart_request_large_file(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
data = BytesIO(b" " * MultiPartParser.spool_max_size * 2)
client = test_client_factory(app)
response = client.post(
"/",
files=[("test_large", data)],
)
assert response.status_code == 200
Copy link
Member

Choose a reason for hiding this comment

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

This test passes on master. We want some condition that proves this PR actually solves the issue.

Copy link

Choose a reason for hiding this comment

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

This type of issue is hard to verify without profiling or adding breakpoints and checking if a write operation that caused a rollover happened on the main thread. I do not know if it is feasable or even possible to provide a unit test that fails if the blocking rollover happens on the wrong thread, that would require assert statements and additional logic in the actual code.

Copy link
Member

Choose a reason for hiding this comment

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

Then we can remove the test, can't we?

Copy link
Author

@HonakerM HonakerM Jul 11, 2025

Choose a reason for hiding this comment

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

@Kludex yeah in it's current state it's not doing a whole lot. Let met try editing the test to use a monkey patched SpooledTemporaryFile to ensure the IO thread is separate from the event thread

Copy link

Choose a reason for hiding this comment

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

It does not ensure that there is no blocking call on the event loop, but it does cover the new code paths that would otherwise not be covered.

Copy link
Author

Choose a reason for hiding this comment

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

@Kludex Just pushed the new test! I ended up creating a new class/app to track where rollover was happening. I'm still new to starlette development so please let me know if you have any code-quality suggestions.

HonakerM added 2 commits July 11, 2025 03:54
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
Signed-off-by: Michael Honaker <mchonaker@gmail.com>
@HonakerM HonakerM force-pushed the fix_upload_file_hang branch from 4a27556 to 297c09e Compare July 11, 2025 08:03
@defnull
Copy link

defnull commented Jul 11, 2025

Not for this PR, but at this point I would just move the rollover logic into UploadFile and not use SpooledTemporaryFile at all. That would also save some expensive getattr() calls in the hot path. Maybe an idea for a future PR.

@HonakerM
Copy link
Author

@defnull @Kludex mentioned this elsewhere but PR2925 is probably the long term solution here. It moves all this logic to anyio and gets it out of the hot path

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.

3 participants