Repository navigation
fix(upload): upload large files in chunks - #272
Open
yschoi9733 wants to merge 1 commit into
Open
yschoi9733 wants to merge 1 commit into
yschoi9733 wants to merge 1 commit into
Conversation
Colab runtimes reject Contents API request bodies over 100 MiB with HTTP 400, and `colab upload` sent the whole file base64-encoded in one PUT, so files larger than about 75 MiB could not be uploaded. A file that fits in one request is still sent as a single "chunk": 1 PUT. Larger files are sent in 64 MiB chunks with the Jupyter Contents API chunked protocol (1, 2, ..., -1). A failed chunk raises IncompleteUploadError, and an optional refresh_session callable picks up a renewed runtime proxy token between chunks. Errors are described without the request URL, which contains the proxy token. Also adds a live integration scenario (integration/repro_large_upload) and updates the file management design doc.
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.
Summary
colab uploadsends the whole file, base64-encoded, in a single Contents APIPUT. Colab runtimes reject request bodies over 100 MiB with HTTP 400, so any file larger than about 75 MiB cannot be uploaded. For example, uploading a 100 MiB file with the released 0.8.0 CLI fails with400 Client Error: Bad Request.This PR keeps the single request for every file that fits in one, so those uploads behave exactly as before. Larger files are sent with the Jupyter Contents API chunked protocol, the same one JupyterLab's file browser uses: chunk
1replaces the file, the following chunks are appended, and the last chunk is sent as-1.The trade-off is that chunk
1replaces the destination right away. If a chunked upload fails partway, any previous file at that path is already gone and the remote file is left partial (see Known limitations).Changes
src/colab_cli/contents.pyfits_in_one_request()computes the exact JSON body size for the remote path. A file that fits (up to about 75 MiB, a little less for long paths because the path is part of the body) is sent in one"chunk": 1request, as before.1, including the final-1, so a chunked upload always starts with chunk1.ContentsClienttakes a new optionalrefresh_sessioncallable, whichupload()calls before every chunk after the first, so an upload that outlasts the runtime proxy token picks up a renewed one.colab uploadandcolab editpass a callable that callsstate.get_session(name), which renews the token only when it is close to expiry.IncompleteUploadErrorsays that the remote file may be incomplete and how many chunks were sent. The exception is a 4xx response (including 404) to chunk1: nothing was written, so the original error is raised unchanged. A single-request upload also raises the original error, as before.requestsputs the request URL, including the proxy token, in its error text. To keep the token out of the terminal, the newdescribe_error()keeps only the error type, HTTP status and reason, andIncompleteUploadErroris raised outside theexceptblock so that the original error does not appear as its chained cause.src/colab_cli/commands/files.py:colab uploadprints failures throughdescribe_error(), andcolab uploadandcolab editpassrefresh_session.tests/test_contents.py: unit tests against a fake Contents API server that follows the chunked protocol (chunk order, files that fit, refused and failed chunks, token refresh, the token staying out of error messages).tests/test_cli.py:colab uploadreports a failure without the proxy token.docs/03_file_management.mddescribes this design in place of the planned kernel-streaming fallback, with a log entry.integration/repro_large_upload/test.shis a new live scenario, listed inintegration/README.md(see Testing).Alternatives considered
sizeafter the last chunk: the Contents API reportsos.lstatsizes, so an upload through a symlinked destination would be reported as incomplete.Testing
uv run pytest tests/: 600 passed, 19 skipped.uv run ruff check .passes.uv run ruff format --check .reportstests/test_automation.pyandtests/test_ssh_wire_contract.py, which this PR does not change.uv run bash integration/repro_large_upload/test.shpasses against a CPU runtime. A 100 MiB file is uploaded in chunks and its remote sha256 matches; a 1 MiB file uploaded to the same path then replaces it completely. With the released 0.8.0 CLI, the same 100 MiB upload fails with HTTP 400./content/p_edge.bin, a 78,643,113-byte file (a request body of exactly 104,857,600 bytes, that is 100 MiB) uploaded in one request, and a 78,643,116-byte file was refused with HTTP 400 (base64 rounds up to groups of 4 characters, so files 1 to 3 bytes larger all give the same 104,857,604-byte body). With this branch, both the 78,643,113-byte file (one request) and a file one byte larger (two chunks) arrive with a matching sha256.Known limitations
1, any previous file at that path has already been replaced and the remote file is left partial, so the upload has to be run again. Uploads that fit in one request are not affected.colab editstill deletes its local temporary copy when the upload fails, as before. For a file too large for one request, an upload that fails after chunk1now also leaves the remote file partial, so both the edits and the original remote content are lost.colab ls,colab rm,colab downloadand thecolab edittraceback can still show the request URL with the proxy token. feat: add -r/--recursive to colab upload and download #267 rewrites theHTTPErrorraised by_requestso that it no longer contains the URL, which covers HTTP errors in these commands but not connection errors (see Overlap with feat: add -r/--recursive to colab upload and download #267).colab stop; it does not look for an unnamed ([?]) assignment left on the server.Overlap with #267
#267 adds
-r(recursive upload and download) and changes the same five files:src/colab_cli/contents.py,src/colab_cli/commands/files.py,tests/test_contents.py,tests/test_cli.pyanddocs/03_file_management.md. The conflicts are mechanical but in several places:contents.py, add toContentsClient.__init__, add methods right afterupload(), append tests, and add a log entry at the top of the design doc.colab edit, feat: add -r/--recursive to colab upload and download #267 replaces theContentsClient(s)line that this PR changes and creates a new client for the upload after the editor closes; that client needsrefresh_sessiontoo._requestto arequests.Session, so this PR's new tests must patchrequests.Session.requestinstead ofrequests.request.describe_error()is still useful after #267, because #267 only rewrites theHTTPErrorfrom_request, and otherrequestserrors (for exampleConnectionError) still carry the URL. Whichever PR is merged second needs a rebase; if that is this one, I will do it.