Repository navigation
fix(engine): retry when a connection closes early - #7
Conversation
troke12
left a comment
There was a problem hiding this comment.
Two things:
- The description is a bit off: a connection that really closes early with a Content-Length already gives
io.ErrUnexpectedEOF, whichmainretries. What this changes is the cleanio.EOFshort 206 case, whichmainalready continued (unbounded, no backoff) viatakeChunk. - Regression:
attempt/backoff are never reset when an attempt made progress. A server that caps each 206 at e.g. 1 MiB finishes a 100 MiB chunk onmain, but fails here after 8 responses with "connection closed early". Please count a short body as a failed attempt only when no bytes were written (pos == start), or resetattempton progress.
TestShortBodyRetriesFromLastByte also passes on main. And once #8 lands, the probe (bytes=0-0) is counted as a transfer and TestShortBodyStopsAfterMaxRetries sees MaxRetries+1.
Note for the whole set (#1–#11): thanks a lot for these! They are all branched from 5350f79 independently, so every pair conflicts on CHANGELOG.md and most on download.go. main has also moved (e830f5d adds a stream downloader that touches download.go). Could you rebase onto current main and stack them in this order: #1 → #2 → #10 → #8 → #5/#6/#7/#11? #2, #3 and #10 each add their own pass loop / transferCancel in Start; one shared design would be much easier to merge.
A response that ends before its chunk is finished is a failed attempt. The chunk is retried from the last byte written, with the usual pause between tries, and stops after the retry limit.
Review changes on top of the previous commit: - A clean io.EOF before the chunk's end is errShortBody only when the response wrote no byte. With progress, fetch returns nil and the worker takes the chunk up again from its last byte, as on main: a server that caps each 206 (at 1 MiB, say) finishes a large chunk instead of failing after MaxRetries responses. TestCappedResponsesFinish covers it. - An empty 206 is what this fixes: main asked again at once, without backoff or limit (29,184 requests in 5 s in the test). It now backs off and stops after MaxRetries (TestEmptyShortBodyStopsAfterMaxRetries). - TestShortBodyRetriesFromLastByte asserts the second request asks for bytes=1024-, and the tests reuse testRange and closeEngineFile. - The comment, README and CHANGELOG describe the clean-EOF case; a body cut short against its Content-Length was already retried as io.ErrUnexpectedEOF. ✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
774597e to
cf6be95
Compare
|
Rebased onto current |
Review changes on top of the previous commit: - TestProbeFirstByteSingleChunk covers the regression the review found: a server answering 206 to bytes=0-0 but 200 to a range over the whole file, with one connection or a file too small to split. With troke12#10 on main the whole-file 200 is kept, so it completes in one request. - Test servers no longer tell the probe by its open-ended range, which bytes=0-0 broke (troke12#1, troke12#7 and troke12#10 fixtures): isProbe and probeRange in engine_test.go, and testRange reports the probe as not explicit. - The PR's test helper is renamed testWholeSpan (wholeFileRange is now a production function) and the test moves to probe_byte_test.go, as probe_test.go exists on main. ✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
Review changes on top of the previous commit: - TestProbeFirstByteSingleChunk covers the regression the review found: a server answering 206 to bytes=0-0 but 200 to a range over the whole file, with one connection or a file too small to split. With #10 on main the whole-file 200 is kept, so it completes in one request. - Test servers no longer tell the probe by its open-ended range, which bytes=0-0 broke (#1, #7 and #10 fixtures): isProbe and probeRange in engine_test.go, and testRange reports the probe as not explicit. - The PR's test helper is renamed testWholeSpan (wholeFileRange is now a production function) and the test moves to probe_byte_test.go, as probe_test.go exists on main. ✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
fetchWithRetry counted every failed attempt against MaxRetries, so a connection that brought some bytes and then stalled, again and again, failed after 8 stalls although each one moved the chunk forward: the same hole #7 closed for short bodies, on the stall path. An attempt that stored bytes now restarts the count and the backoff; attempts with no progress still run out (TestStallsWithoutProgressRunOut). ✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
Summary
A server that answers a range with a clean but short 206 (Content-Length matching the shortened body, so the client sees
io.EOF) used to be asked again at once by the worker loop, with no backoff and no limit. With an empty 206 that is a tight loop: in the test,mainsends 29,184 requests in 5 seconds.A short response that wrote no bytes now returns
errShortBody, so it goes through the normal retry path (backoff,MaxRetries). A short response that made progress is not a failure: the worker continues the chunk from its last byte, exactly as onmain, so a server that caps each 206 still finishes a large chunk.A connection cut mid-body against its Content-Length was already retried (
io.ErrUnexpectedEOF); that path is unchanged.Commits
0b7d9e4(Ryan Bagas): the original change, rebased onto currentmain(after fix(engine): retry a connection that stops sending #1 and fix(engine): request raw bytes for a ranged download #5).cf6be95: review changes. A short body counts as failed only whenpos == start; tests rewritten to tell this apart frommain; description/README/CHANGELOG corrected.Test plan
go test ./...andgo test -race ./internal/engineTestShortBodyRetriesFromLastByte: the second request asks forbytes=1024-…TestCappedResponsesFinish: every 206 capped at 1 KiB, 128 responses, completes. This test fails on the original version of this PR (stops after 8).TestEmptyShortBodyStopsAfterMaxRetries: an empty 206 errors after exactlyMaxRetriestransfers. This test fails onmain(29,184 requests, then the 5 s test deadline).Heads-up for #8
The test servers here and in #1 detect the probe by its open-ended range (
bytes=0-). #8 changes the probe tobytes=0-0, so these fixtures will need to tell the probe apart another way when #8 is rebased.✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)