fix(client): stream parallel-upload slices instead of buffering in memory - #4
Merged
Merged
Conversation
…mory Parallel uploads read each byte-range slice fully into memory via `io.BytesIO(f.read(length))`, so with `parallel_uploads=N` the in-flight slices summed to the whole file — an OOM risk for large files. Add `FileSlice`, a read-only, seekable, range-bounded file view that reads on demand. The uploader pulls one `chunk_size` block at a time, so memory use is one chunk per active slice instead of the whole slice. Wired into both the sync (`parallel.py`) and async (`aio/client.py`) paths for parity; the uploader treats the injected stream as not-owned, so each path closes its FileSlice explicitly. Closes #3. Co-Authored-By: Claude Opus 4.8 (1M context) <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.
Purpose
Closes #3. Parallel uploads read each byte-range slice fully into memory (
io.BytesIO(f.read(length))), so withparallel_uploads=Nthe in-flight slices summed to the whole file — an OOM risk for large files. Raised by gemini-code-assist on #1, deferred from #2.Changes
resumable_upload/client/_fileslice.py—FileSlice, a read-only, seekable, range-bounded (io.RawIOBase) view that reads on demand. The uploader pulls onechunk_sizeblock at a time, so memory use is one chunk per active slice instead of the whole slice.client/parallel.pyand asyncclient/aio/client.py(replacing the old_read_slice+BytesIO).FileSliceexplicitly — the uploader treats an injected stream as not-owned and will not close it.TUS compliance
Client-side only. No change to wire bytes, headers, status codes, or offset semantics — same bytes are sent, just read incrementally from disk.
Test Plan
pytest -q ruff check resumable_upload tests && ruff format --check resumable_upload tests ty check resumable_uploadTest Result
pytest— 503 passed, 4 skipped (+5 newFileSliceunit tests; existing sync+async parallel/concat suites now exerciseFileSliceend-to-end)ruff check/ruff format --check/ty check— clean🤖 Generated with Claude Code