Skip to content

fix(upload): upload large files in chunks - #272

Open
yschoi9733 wants to merge 1 commit into
googlecolab:mainfrom
yschoi9733:feat/chunked-upload
Open

yschoi9733 wants to merge 1 commit into
googlecolab:mainfrom
yschoi9733:feat/chunked-upload

Conversation

@yschoi9733

Copy link
Copy Markdown

Summary

colab upload sends the whole file, base64-encoded, in a single Contents API PUT. 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 with 400 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 1 replaces the file, the following chunks are appended, and the last chunk is sent as -1.

The trade-off is that chunk 1 replaces 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.py
    • fits_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": 1 request, as before.
    • A larger file is read and sent in 64 MiB chunks (about 85 MiB once base64-encoded). The server appends every chunk other than 1, including the final -1, so a chunked upload always starts with chunk 1.
    • ContentsClient takes a new optional refresh_session callable, which upload() calls before every chunk after the first, so an upload that outlasts the runtime proxy token picks up a renewed one. colab upload and colab edit pass a callable that calls state.get_session(name), which renews the token only when it is close to expiry.
    • If a chunked upload fails, a new IncompleteUploadError says that the remote file may be incomplete and how many chunks were sent. The exception is a 4xx response (including 404) to chunk 1: nothing was written, so the original error is raised unchanged. A single-request upload also raises the original error, as before.
    • requests puts the request URL, including the proxy token, in its error text. To keep the token out of the terminal, the new describe_error() keeps only the error type, HTTP status and reason, and IncompleteUploadError is raised outside the except block so that the original error does not appear as its chained cause.
  • src/colab_cli/commands/files.py: colab upload prints failures through describe_error(), and colab upload and colab edit pass refresh_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 upload reports a failure without the proxy token.
  • docs/03_file_management.md describes this design in place of the planned kernel-streaming fallback, with a log entry.
  • integration/repro_large_upload/test.sh is a new live scenario, listed in integration/README.md (see Testing).

Alternatives considered

  • Streaming through the kernel (the fallback planned in the design doc): the Contents API already supports chunked uploads, so no kernel code is needed.
  • Uploading to a temporary name and renaming it at the end: this would keep the old file intact while the chunks are sent. But the API refuses to rename onto an existing path (HTTP 409), so the old file would have to be deleted first. If the connection drops between the delete and the rename, the old file is gone and the new data is left under the temporary name. Deleting and renaming would also replace a symlinked destination with a regular file, whereas a single-request save writes through the symlink to its target. This PR writes the chunks straight to the destination instead, as JupyterLab's file browser does.
  • Checking the stored size after the last chunk: the Contents API reports os.lstat sizes, 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 . reports tests/test_automation.py and tests/test_ssh_wire_contract.py, which this PR does not change.
  • uv run bash integration/repro_large_upload/test.sh passes 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.
  • The 100 MiB limit was measured earlier on a CPU runtime with the 0.6.0 CLI, whose upload request is the same as 0.8.0's. For /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

  • If a chunked upload fails or is interrupted (for example with Ctrl-C) after chunk 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 edit still deletes its local temporary copy when the upload fails, as before. For a file too large for one request, an upload that fails after chunk 1 now also leaves the remote file partial, so both the edits and the original remote content are lost.
  • Requests still have no timeout, so a stalled chunk can hang the upload (see Discussion `colab upload`/`download` can hang forever: Contents API requests have no timeout #252).
  • colab ls, colab rm, colab download and the colab edit traceback can still show the request URL with the proxy token. feat: add -r/--recursive to colab upload and download #267 rewrites the HTTPError raised by _request so 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).
  • The new integration scenario checks that its own session is gone after 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.py and docs/03_file_management.md. The conflicts are mechanical but in several places:

  • Both PRs add imports and module-level helpers at the top of contents.py, add to ContentsClient.__init__, add methods right after upload(), append tests, and add a log entry at the top of the design doc.
  • In colab edit, feat: add -r/--recursive to colab upload and download #267 replaces the ContentsClient(s) line that this PR changes and creates a new client for the upload after the editor closes; that client needs refresh_session too.
  • feat: add -r/--recursive to colab upload and download #267 moves _request to a requests.Session, so this PR's new tests must patch requests.Session.request instead of requests.request.

describe_error() is still useful after #267, because #267 only rewrites the HTTPError from _request, and other requests errors (for example ConnectionError) still carry the URL. Whichever PR is merged second needs a rebase; if that is this one, I will do it.

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