Skip to content

fix: upload every chunk once in LargeFileUploadTask - #1172

Open
nabeleon94 wants to merge 1 commit into
microsoftgraph:mainfrom
nabeleon94:fix/large-file-upload-open-ended-range
Open

nabeleon94 wants to merge 1 commit into
microsoftgraph:mainfrom
nabeleon94:fix/large-file-upload-open-ended-range

Conversation

@nabeleon94

@nabeleon94 nabeleon94 commented Nov 28, 2026 •

Copy link
Copy Markdown

Overview

Fixes microsoftgraph/msgraph-sdk-python#1631. Also fixes #718 (and microsoftgraph/msgraph-sdk-python#1067).

LargeFileUploadTask.upload() fails for any file larger than one chunk, and it errors after the upload has already finished for files that fit in one chunk.

  • Multi-chunk uploads: after each chunk the loop set self.next_range = next_range[0] + "-". The service sends nextExpectedRanges as "535313" (Outlook) or "535313-" (OneDrive), so next_chunk()/last_chunk() then read an empty end and called int(''). The ValueError was logged and swallowed for the middle chunks, so no further chunks were sent, and last_chunk() then raised invalid literal for int() with base 10: ''. That is the traceback in #1631.
  • Single-chunk uploads: the first chunk went through next_chunk() before the loop, and the chunks == 0 iteration then sent the same range again through last_chunk(). By then the session was complete, so the service returned the 400 in LargeFileUploadTask.upload() throws a 400 error when file size is smaller than max_chunk_size #718 / chore(deps): consolidate all dependabot updates #1067.

Changes

  • upload() now starts at the next expected range (0 for a new session, the stored range for resume()). It sends each chunk once, uses last_chunk() only for the final chunk so item_response still holds the parsed item, and continues from the service's nextExpectedRanges. Upload errors now propagate instead of being logged and dropped.
  • A small _parse_range() helper handles "0-99", "100-" and "100" (and a list), treating a missing end as 0. next_chunk() and last_chunk() already read 0 as "up to one chunk from start". Those two methods use it instead of their duplicated int() parsing.
  • The unused logging import is removed.

Testing Instructions

  • New tests/tasks/test_large_file_upload.py. It uses a fake upload session that checks each Content-Range against the bytes it has received, rejects PUTs after completion, and answers nextExpectedRanges in both the Outlook ("N") and OneDrive ("N-") forms.
    • Uploads of 1, 100, 320, 321, 1000 and 960 bytes with a 320-byte chunk: each byte is sent exactly once, the callback gets each range, and item_response/location are set.
    • resume() from "640-" and "640" continues at byte 640.
    • _parse_range cases.
  • On main, 14 of the 14 upload/resume tests fail: 8 with ValueError: invalid literal for int() with base 10: '' and 6 with the PUT-after-completion error. With this change, all 18 pass.
  • pytest tests: 102 passed. The 4 test_graph_client_factory.py failures happen on main too in my environment and are unrelated.
  • yapf -dr src, isort on the changed file, pylint (10.00/10 on the file) and mypy (no new findings) are clean.
  • Not run against a live Graph tenant.

upload() appended "-" to the service's nextExpectedRanges value and then
parsed the empty end with int(), so every upload larger than one chunk
failed with "invalid literal for int() with base 10: ''". The error was
logged and swallowed for the middle chunks, and raised from last_chunk().
Uploads that fit in one chunk sent the same range a second time after the
session had completed, which the service rejects with a 400.

upload() now walks the file from the next expected range, sends each
chunk once, uses last_chunk() only for the final chunk, and lets upload
errors propagate. Open-ended ranges ("100-" or "100") are parsed with a
missing end treated as 0, as next_chunk() and last_chunk() already expect.
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant