feat(files): streamed-to-disk upload (OOM fix) + 413 fast-reject - #104
Open
ianu82 wants to merge 1 commit into
Open
feat(files): streamed-to-disk upload (OOM fix) + 413 fast-reject#104ianu82 wants to merge 1 commit into
ianu82 wants to merge 1 commit into
Conversation
Uploads previously did `await upload.read()`, buffering the entire file in RAM before the size cap was checked — a large upload could OOM the server. Replace with a chunked stream-to-disk ingest that enforces the cap incrementally. - services/files.py: new `stream_upload_to_path()` copies the UploadFile to disk one 1 MiB chunk at a time, aborting the instant the running total crosses `max_upload_bytes` (it stops reading — never drains the rest of a huge stream) and removing the partial file. The MIME/extension allow-list is split into `validate_upload_type()` and checked up front, before a single byte is read. `create_file()` now streams instead of reading whole. `reject_if_content_length_over_cap()` + `UploadTooLarge` add a cheap Content-Length fast-reject (mapped to 413) with a 1 MiB multipart allowance so under-cap files are never falsely rejected. - Client-facing upload paths route through the streamed ingest: the attachments compat bridge and the OpenAI-style /files endpoint enforce the type allow-list + cap; the project-files upload streams + caps but keeps accepting arbitrary types (it's the project working dir, where source/config/archive files are expected — no chat-attachment allow-list). - Mid-stream cap breach -> 400; declared-oversize Content-Length -> 413. - tests: streaming unit test proves early abort (a 10x-cap stream pulls only ~one chunk past the cap, leaves no partial file), under-cap still writes fully, the endpoint leaves no orphaned file on rejection, plus Content-Length 413 coverage. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces
await upload.read()(whole file in RAM) with chunked streamed-to-disk ingest. The size cap is enforced mid-stream — the instant bytes exceed the cap it stops reading (peak memory ~1 MiB), unlinks the partial, and returns 400; an obviously-oversizedContent-Lengthis rejected up front as 413; the MIME allow-list is checked before any byte is read. Covers all three upload paths. (Code review caught and reverted applying the chat allow-list to project-files — that's the working dir and should accept arbitrary types.)Testing: +12 tests (10×-cap stream aborts after ~one chunk with no partial; under-cap writes fully; no orphan on rejection; Content-Length 413); full suite 307 passed / 8 pre-existing failures.
🤖 Generated with Claude Code