Skip to content

fix(engine): continue on one connection when a range is ignored - #10

Merged
troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-ignored-range
Oct 19, 2026
Merged

troke12 merged 2 commits into
troke12:mainfrom
rigaz29:engine-ignored-range

Conversation

@rigaz29

@rigaz29 rigaz29 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Some servers advertise ranges in the probe but answer a smaller range with 200 and the whole file. On main that chunk failed with "server ignored Range" after 8 retries. Now the bytes already stored are kept, the other connections stop, and the rest of the file is fetched on one connection: the stored prefix of the 200 body is skipped and the remainder is written at its offset. The download stays on one connection after a pause/resume (alone is persisted).

One design with If-Range (#2)

This runs in the pass loop #2 added. It does not add a second loop.

  • A 200 to a ranged request:
  • A shorter Content-Length than the file is still an error.
  • Resume: a probe that finds ranges gone, with the same validator, keeps the prefix and continues on one connection. Before, feat(engine): restart a download when the file on the server changes #2 would have discarded the prefix.
  • After an ignored range the next pass reuses the plan without probing again. A changed file still re-probes, and a second change is still an error.

Commits

  • 98f1863 (Ryan Bagas): the original change, ported onto feat(engine): restart a download when the file on the server changes #2's pass loop (transferCancel, worker, fetch 200 handling, state). The two if_range_test/ignored_range_test cases fixed in the next commit fail at this commit.
  • 237c080: review changes. Validator check before the one-connection path, keep prefix on range loss at resume, discardPrefix through the idle timer, test fixes.

Known limits

  • When the server sends no ETag and no Last-Modified, a replaced file of the same size cannot be detected. This is the reviewer's "no If-Range sent" case.
  • Not done (minor from review): when chunk 0 itself gets the 200, its body is dropped and the remainder is fetched again, instead of continuing that same body past the chunk's end.

Test plan

  • go test ./... and go test -race ./internal/engine
  • TestSubrangeOKWithOtherETagSameSizeRestarts: a same-size replacement with a new ETag restarts and gives the new file. Fails without the validator check.
  • TestSubrangeOKWithSameETagContinuesAlone: the stored prefix is kept. The 200 body's first half differs, so the result shows it was skipped.
  • TestIgnoredSubrangeKeepsFinishedBytes: resume against a server that ignores Range entirely; no transfer starts before the stored prefix. Fails if range loss discards the prefix.
  • TestDiscardPrefixStallRetries: a stall while skipping the prefix is retried. Hangs (fails) with an untimed read.
  • TestSubrangeOKFallsBackToOneConnection, TestPlanAloneKeepsContiguousPrefix, TestAloneSurvivesRestore, TestContiguousDoneStopsAtTheFirstHole

✨ 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.

This is the cleaner of #3/#10 and the tests are good. Requests:

  • Silent corruption risk: the 200 path only checks Content-Length == Total. If the file was replaced with one of the same size, this writes the new tail after the old prefix (on main it errors). Please compare the 200's validator with the probe's — easiest after rebasing onto #2, and take the one-connection path only when the validator matches (or no If-Range was sent).
  • Reading the stored prefix via discardPrefix (and the body read in #3) isn't covered by #1's idle timeout; route it through the timed read when rebasing.
  • The resume test's server returns 500 for any request starting at byte 0, which will break once #2's re-probe (bytes=0-) lands.

Minor: when chunk 0 itself gets the 200, the full body is discarded and fetched again.


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.

@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.

This is the cleaner of #3/#10 and the tests are good. Requests:

  • Silent corruption risk: the 200 path only checks Content-Length == Total. If the file was replaced with one of the same size, this writes the new tail after the old prefix (on main it errors). Please compare the 200's validator with the probe's — easiest after rebasing onto #2, and take the one-connection path only when the validator matches (or no If-Range was sent).
  • Reading the stored prefix via discardPrefix (and the body read in #3) isn't covered by #1's idle timeout; route it through the timed read when rebasing.
  • The resume test's server returns 500 for any request starting at byte 0, which will break once #2's re-probe (bytes=0-) lands.

Minor: when chunk 0 itself gets the 200, the full body is discarded and fetched again.


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 10:11
A 200 for a smaller range is the whole file, so writing it at the
range offset would corrupt the partial. Keep the contiguous prefix
and fetch the remainder on one connection.
Review changes on top of the previous commit, which ports troke12#10 onto the
pass loop If-Range (troke12#2) added, as one design:

- The one-connection path for a whole-file 200 runs only when its
  validator matches the one sent as If-Range, or none was sent. A
  replaced file of the same size used to be joined to the old prefix;
  it now restarts (TestSubrangeOKWithOtherETagSameSizeRestarts).
- A resume probe that finds ranges gone is no longer a changed file by
  itself: with the same validator the prefix is kept and the download
  continues on one connection, as when a transfer notices it.
  representationDiffers loses its range clause.
- discardPrefix reads through the idle timer, so a stall while skipping
  the stored prefix is retried (TestDiscardPrefixStallRetries).
- Tests: the ignored-range resume fixture answers the resume probe
  (bytes=0-); troke12#2's same-ETag subrange test now expects completion on one
  connection with the stored prefix kept.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
@troke12
troke12 force-pushed the engine-ignored-range branch from 45eeab5 to 237c080 Compare October 19, 2026 03:15
@troke12

troke12 commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Rebased onto current main (with #2 merged) and ported into #2's pass loop as one design: a whole-file 200 takes your one-connection path only when its validator matches the If-Range one (or none was sent); a different one restarts. A resume probe that finds ranges gone now keeps the prefix too, and discardPrefix reads through the idle timer. Your commit is kept with you as author (with the port onto #2), review changes are a separate commit on top. Thanks!

@troke12
troke12 merged commit b3950dd 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 to rigaz29/odm that referenced this pull request Oct 19, 2026
Review changes on top of the previous commit, which ports troke12#11 onto the
pass loop on main:

- The next connection opens once file bytes arrive on any connection
  after the last one opened, not once every opened connection has had
  bytes. A connection that never got its first byte froze the ramp for
  good, where main opened all N; now the others keep it going
  (TestRampPassesAStalledConnection). Without a timer, a server that
  refuses every transfer is still not asked on more connections.
- ramp runs as its own goroutine holding a count on the pass's
  WaitGroup, and ends when every connection is open, the pass ends, or
  nothing is left to hand out (hasWork). A worker error no longer stops
  it: the run may still complete.
- One-connection mode (troke12#10) and !RangeOK open a single connection.
- Tests use no sleeps to show something did not happen: only file bytes
  open a connection, so while bytes are held back none can. They count
  opened connections (rampOpened), not requests, and tell the probe by
  bytes=0-0 (isProbe).

✨ 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, which ports #11 onto the
pass loop on main:

- The next connection opens once file bytes arrive on any connection
  after the last one opened, not once every opened connection has had
  bytes. A connection that never got its first byte froze the ramp for
  good, where main opened all N; now the others keep it going
  (TestRampPassesAStalledConnection). Without a timer, a server that
  refuses every transfer is still not asked on more connections.
- ramp runs as its own goroutine holding a count on the pass's
  WaitGroup, and ends when every connection is open, the pass ends, or
  nothing is left to hand out (hasWork). A worker error no longer stops
  it: the run may still complete.
- One-connection mode (#10) and !RangeOK open a single connection.
- Tests use no sleeps to show something did not happen: only file bytes
  open a connection, so while bytes are held back none can. They count
  opened connections (rampOpened), not requests, and tell the probe by
  bytes=0-0 (isProbe).

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
@rigaz29
rigaz29 deleted the engine-ignored-range 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