Skip to content

fix(engine): retry when a connection closes early - #7

Merged
troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-short-body
Oct 19, 2026
Merged

troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-short-body

Conversation

@rigaz29

@rigaz29 rigaz29 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

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, main sends 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 on main, 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

Test plan

  • go test ./... and go test -race ./internal/engine
  • TestShortBodyRetriesFromLastByte: the second request asks for bytes=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 exactly MaxRetries transfers. This test fails on main (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 to bytes=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)

@troke12 troke12 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Two things:

  • The description is a bit off: a connection that really closes early with a Content-Length already gives io.ErrUnexpectedEOF, which main retries. What this changes is the clean io.EOF short 206 case, which main already continued (unbounded, no backoff) via takeChunk.
  • 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 on main, 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 reset attempt on 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.

rigaz29 and others added 2 commits October 19, 2026 09:37
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)
@troke12

troke12 commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Rebased onto current main (after #1 and #5) and applied the review: a clean short 206 now counts as a failed attempt only when it wrote no bytes, so servers that cap each response keep working, while an empty 206 backs off and stops after MaxRetries (on main it looped without limit). Tests now fail on main and on the original version respectively. Your commit is kept with you as author; the review changes are a separate commit on top. Thanks!

@troke12
troke12 merged commit 038290f into troke12:main Oct 19, 2026
5 checks passed
troke12 pushed a commit to rigaz29/odm that referenced this pull request Oct 19, 2026
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)
troke12 pushed a commit that referenced this pull request Oct 19, 2026
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)
troke12 pushed a commit that referenced this pull request Oct 19, 2026
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)
@rigaz29
rigaz29 deleted the engine-short-body branch October 19, 2026 05:01
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.

2 participants