Skip to content

Installer downloads: retry a body cut short or trickling instead of hashing it - #15763

Merged
danielhanchen merged 2 commits into
mainfrom
installer-downloads-detect-truncation
Oct 28, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
installer-downloads-detect-truncation

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Summary

The macOS Chat UI, API, Update and GGUF inference job fails on unrelated PRs (seen on #15696 and #15673 today) after 50 to 75 minutes in Studio setup:

[node-prebuilt] downloading https://nodejs.org/dist/v24.22.1/node-v24.22.1-darwin-arm64.tar.gz
Downloading node-v24.22.1-darwin-arm64.tar.gz:  12.1% (6.1 MiB/50.6 MiB) at 1.6 KiB/s
[node-prebuilt] node-v24.22.1-darwin-arm64.tar.gz checksum mismatch 1/2: expected=89c6... actual=8014...
...
[node-prebuilt] prebuilt unavailable: node-v24.22.1-darwin-arm64.tar.gz checksum mismatch after retry
error          studio setup failed (exit code 1)

The archive was never tampered with: it was 6.1 MiB of a 50.6 MiB body. Two installer bugs turn a flaky connection into that:

  1. Truncated bodies are accepted. http.client.HTTPResponse.read(amt) returns b"" when the server closes before Content-Length bytes arrive, without raising IncompleteRead (CPython leaves this as a known compatibility trade-off). The copy loops in install_node_prebuilt.download_file, prebuilt_core.download_file and prebuilt_core.download_bytes stop at the first empty read, so a short file is written out and hashed. For Node that surfaces as a misleading checksum mismatch; for a llama.cpp asset with no published digest the short archive goes straight to the extractor.
  2. A trickle never times out. The 120 s socket timeout is per read, so a connection that keeps delivering a few bytes a second holds an attempt open indefinitely (1.6 KiB/s above, over 30 minutes per attempt).

Change

  • prebuilt_core.copy_response_body: one body-copy helper used by all three loops.
    • Raises TruncatedDownload (a subclass of http.client.IncompleteRead) when the body ends short of Content-Length.
    • Raises StalledDownload (a TimeoutError) when a 60 s window brings under 256 KiB, about 4 KiB/s, and the body is not yet complete.
    • Reads a real http.client.HTTPResponse with read1, which returns what one receive brings. read(n) waits for all n bytes, so on a trickle it would block for hours and the window check would never run.
  • Both exceptions are already classified as retryable by is_retryable_url_error in both modules, so the existing retry loops reconnect instead of hashing a partial file or waiting on a dead connection. Bodies without Content-Length (chunked) keep their current behaviour; http.client already raises on a cut chunked body.

Testing

  • New tests/studio/install/test_download_truncation_and_stall.py (11 tests) runs against a real local HTTP server, because the read(amt) EOF behaviour is what is under test and a BytesIO double cannot reproduce it:
    • a body closed at 25% raises a retryable TruncatedDownload;
    • download_file (Node and llama.cpp paths) and download_bytes reconnect and finish with the full payload;
    • a server that truncates every attempt is refused with no partial file left behind;
    • a trickling body is abandoned, a slow but moving one is not, and a window closing on the final byte is not a stall.
    • a real server sending the full Content-Length then one byte every 50 ms is abandoned within the (shortened) window, and download_file reconnects past it on both paths.
  • Sabotage: with only the truncation check removed, 5 tests fail; with only the stall check removed, the trickle test fails; with plain read instead of read1, the three real-trickle tests fail (the call blocks until the server gives up).
  • tests/studio/install base vs head on this host: identical failing test IDs (21, all pre-existing on main here), 10274 passed at base and 10282 with this change (the 8 new tests).
  • Real download through the new path: node-v24.22.1-linux-x64.tar.gz (55.6 MiB) from nodejs.org verified against the pinned sha256 at 378 MiB/s, so the smaller reads cost nothing.

…ashing it

http.client's read(amt) returns b"" when the server closes before
Content-Length, so the Node and llama.cpp prebuilt download loops accepted a
truncated archive and reported it as a checksum mismatch (or, with no
published digest, handed it to the extractor). A body trickling in at a few
KiB/s never trips the per-read socket timeout, so one stalled connection held
macOS setup for over an hour.

copy_response_body in prebuilt_core now raises TruncatedDownload (an
IncompleteRead) when the body ends short and StalledDownload (a TimeoutError)
when a 60 s window brings under 256 KiB; both are already retryable, so the
existing loops reconnect.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T13:47:46.267599Z a229b16 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6d8e0062d6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/prebuilt_core.py Outdated
window_start = time.monotonic()
window_bytes = 0
while True:
chunk = response.read(chunk_size)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Let the stall watchdog run before a full chunk arrives

For a real HTTP response, read(65536) waits for the entire requested amount (or EOF), rather than returning each available fragment as the _Trickle test double does. A server delivering one byte per second therefore blocks this call for roughly 18 hours while continually resetting the socket timeout, so the new 60-second stall check cannot run and installation still hangs. A local HTTP-server reproduction with a shortened watchdog window confirms that the check only executes after the server closes. Use an available-data read such as read1, with appropriate deadline handling, and exercise the stall test against a real streaming response.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the _Trickle double returned whatever it had, which a real HTTPResponse.read(65536) does not: it waits for the full 64 KiB, so a slow sender kept the watchdog from ever running.

Fixed in a229b16: a real http.client.HTTPResponse is now read with read1, which returns what one receive brings, so the window check runs at least once per socket timeout. Other bodies (file:// mirrors, test doubles) keep plain read.

Added a stall test against a real local server that sends the full Content-Length header and then one byte every 50 ms, plus download_file (Node and llama.cpp paths) reconnecting past it. With the old read all three fail: the call blocks until the server gives up after 10 s and reports 200 bytes in the last 10s. With read1 it is abandoned within the 0.3 s test window and the retry completes. A real nodejs.org download through the new path still verifies (55.6 MiB in 0.24 s).

HTTPResponse.read(n) waits for all n bytes, so a connection sending one
byte a second blocked a 64 KiB read for hours and the window check never
ran. read1 returns what one receive brings. Adds a stall test against a
real trickling server; with plain read it blocks until the server gives up.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: a229b16df5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 93426bf into main Oct 28, 2026
3 of 65 checks passed
@danielhanchen
danielhanchen deleted the installer-downloads-detect-truncation branch October 28, 2026 13:53
danielhanchen added a commit that referenced this pull request Oct 28, 2026
)

## Summary

#15763 broke `tests/python/test_windows_setup_download_progress.py` on
main (seen on the #15693 staging run; reproduces locally wherever `pwsh`
is installed):

```
test_windows_real_download_reports_progress_while_active[node|llama|whisper]
E   assert [0.3, 5.2, 10.0, 15.0, 20.0] == [5, 10, 15, 20]
```

To let the new stall window fire on a trickling connection, #15763 reads
real HTTP responses with `read1`, which returns what one receive brings.
It also moved the progress update onto every receive, so the first
percent line now appears at 0.3% and the milestones print at whatever
receive crosses them (5.2%) instead of at the whole 1 MiB reads the
installers used before. The progress text the Windows setup scripts
relay changed shape, which is what this test pins.

## Change

`prebuilt_core.copy_response_body`:

- the chunk size is back to 1 MiB;
- on the `read1` path each read asks only for the bytes up to the next 1
MiB boundary, and progress is reported when the boundary is reached, so
the updates land exactly where whole-chunk reads put them;
- the stall window is still checked after every receive, so a trickle is
still abandoned within the window;
- plain `read` bodies (file:// mirrors, test doubles) read whole chunks
and report each one, as before #15763.

## Testing

- All 23 test files that touch the installer downloaders, main vs this
branch: the only difference in failing IDs is the three
`test_windows_real_download_reports_progress_while_active` cases, which
now pass. The 21 `tests/studio/install/test_dependency_pass_skips.py`
failures and the `tests/utils/aime_eval.py` collection error are on both
sides on this host.
- New `test_progress_on_a_real_response_lands_on_whole_chunks`: a real
local server streams 4 MiB in 48 KiB writes and progress must be updated
at exactly 1, 2, 3 and 4 MiB. It runs without `pwsh`, so Linux CI covers
the shape too.
- Sabotage: reporting progress on every receive again fails the new test
and all three PowerShell cases.
- The trickle tests from #15763 still pass (the stall is detected within
the shortened window), and a real nodejs.org download verifies (55.6 MiB
in 0.27 s).
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