Skip to content

🐛 fix(standalone): resume Python downloads cut short of Content-Length - #2453

Merged
gaborbernat merged 3 commits into
pypa:mainfrom
HarshJamgaonkar05:fix/resume-truncated-python-download
Nov 16, 2026
Merged

gaborbernat merged 3 commits into
pypa:mainfrom
HarshJamgaonkar05:fix/resume-truncated-python-download

Conversation

@HarshJamgaonkar05

@HarshJamgaonkar05 HarshJamgaonkar05 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2452.

A dropped connection while --fetch-python downloaded a standalone Python build ended in a checksum mismatch instead of a resumed download. 🌐 Python's HTTP client returns a body cut short of its Content-Length as if it were complete (python/cpython#115997), so pipx stopped at the partial archive and never used the Range resume it already has for connections that fail outright.

pipx now checks after each attempt whether the server sent fewer bytes than it announced. A short body counts as the same transient network error as a reset connection, so it goes through the existing retry with backoff (three attempts in total) and asks the server for the rest with a Range request; the archive checksum is still verified afterwards. Script downloads got the same check in #2251.

@AP24110011695

Copy link
Copy Markdown
Contributor

Reviewed this against the resume loop as it stands on main — the mechanics check out:

  • the length-shortfall raise covers exactly the case the loop misses today: read(32768) returning short-but-nonempty keeps the iterator going until a b"", and a connection cut before that used to end the download silently and shift the failure to the checksum stage; IncompleteRead is already in _TRANSIENT_NETWORK_ERRORS, so the new raise lands in the existing retry, and the next attempt resumes from the bytes on disk
  • the checksum in _unpack runs after the assembled file, so even a server that answers a resume range wrongly cannot produce an archive that passes validation
  • the getattr(response, "length", None) default keeps EOF-terminated bodies (no Content-Length) behaving as before, and the 206 case counts the range length, which reaches 0 on a complete resume

One cosmetic nit only: IncompleteRead(b"", missing) reports an empty partial — everything the process wrote is in the file, not in the exception — which only matters if the final-failure message ever prints the exception's payload. Not worth changing unless you want the repr to carry the on-disk count.

The test matrix mirrors the real failure modes (short body, server-ignores-range, resume completion), so this gets my support as-is.

@AP24110011695

Copy link
Copy Markdown
Contributor

5c5f2c0 looks right — the subclass keeps it inside the transient-retry path while carrying the on-disk count, and asserting the retry cause in the test pins the behavior. Good to go from my side.

@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
HarshJamgaonkar05 and others added 3 commits November 16, 2026 13:48
HTTPResponse.read(amt) returns b"" instead of raising when the server
closes before the advertised Content-Length (python/cpython#115997), so
_download_resuming took a dropped connection for a finished download and
failed with a checksum mismatch instead of resuming with a Range request.

Check response.length after writing the body, as download_script does
since pypa#2251, and raise IncompleteRead so the existing backoff resumes
from the bytes already on disk.
…error

IncompleteRead(b"", missing) reported "0 bytes read" although everything
received so far is in the archive on disk, so the retry log and the
cause of the final PipxError understated the progress. Raise a subclass
whose message counts the bytes on disk instead of a partial body.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
The subclass existed only to word the retry log differently, and no
handler tells it apart from IncompleteRead, so raise the built-in one.
The test checks the retry and its Range header instead of the log text.
@gaborbernat
gaborbernat force-pushed the fix/resume-truncated-python-download branch from 169fa66 to e21d91b Compare November 16, 2026 22:02
@gaborbernat
gaborbernat merged commit 2a6ae8a into pypa:main Nov 16, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Standalone Python download never resumes when the connection drops: read() returns short without raising

3 participants