Skip to content

fix(engine): stop counting bytes past a chunk's end - #9

Closed
rigaz29 wants to merge 1 commit into
troke12:mainfrom
rigaz29:engine-clamp-end
Closed

rigaz29 wants to merge 1 commit into
troke12:mainfrom
rigaz29:engine-clamp-end

Conversation

@rigaz29

@rigaz29 rigaz29 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Taking the tail of a chunk while that connection still has bytes in hand no longer counts those bytes twice. Progress stays within the file size.

Behavior

  • Before a buffer is written, the chunk's current end is read again. Bytes past that end are left for the connection that took the tail.
  • After the write, the end is checked once more. Only bytes still inside the chunk are added to its progress.
  • The file bytes already written past the new end are the same bytes the other connection fetches. They are not added to both chunks.

Test plan

  • go test ./... (61 tests)
  • A download that splits a chunk in the middle of a read finishes with progress equal to the file size
  • The bytes on disk match the file from the server

A connection can take the tail of a chunk while another read is still
in hand. Only the bytes that still belong to the chunk are written into
its progress, so the same bytes are not counted twice.

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

Thanks, but I don't think this path is reachable. The only thing that moves ch.End mid-read is Split, which needs Remaining >= 2*MinChunkSize (128 KiB) and sets mid = Next() + Remaining/2, i.e. at least 64 KiB past the current position. The read buffer is exactly 64 KiB, so an in-flight buffer can't cross the new end. #6 keeps the same mid formula.

The test also doesn't hit the new check: the hook moves End before limit := ch.End is read, so the existing pre-write clamp already handles it, and a cut at 1024 isn't something Split can produce. It also adds a test hook (beforeWrite) to the production struct.

I'd rather not take it unless chunk/buffer sizes change. ownedSpan itself is clean.


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 commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Closing per the review above: with the 64 KiB read buffer and Split always cutting at least 64 KiB ahead of the read position, a buffer cannot cross a moved chunk end, so the new check is unreachable today. Worth revisiting if chunk or buffer sizes change. Thanks!

@troke12 troke12 closed this Oct 19, 2026
@rigaz29
rigaz29 deleted the engine-clamp-end 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