Skip to content

feat(files): streamed-to-disk upload (OOM fix) + 413 fast-reject - #104

Open
ianu82 wants to merge 1 commit into
feat/nocloud/attachmentsfrom
feat/nocloud/attachments-stream
Open

feat(files): streamed-to-disk upload (OOM fix) + 413 fast-reject#104
ianu82 wants to merge 1 commit into
feat/nocloud/attachmentsfrom
feat/nocloud/attachments-stream

Conversation

@ianu82

@ianu82 ianu82 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

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-oversized Content-Length is 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.

⚠️ Stacked on #97 (upload guardrails) → #78.

🤖 Generated with Claude Code

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>
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