Skip to content

Fix chunk index mismatch between getSliceChunkFile and checkChunkSize#50

Merged
sendya merged 3 commits intomainfrom
copilot/fix-chunk-size-check
Mar 17, 2026
Merged

Fix chunk index mismatch between getSliceChunkFile and checkChunkSize#50
sendya merged 3 commits intomainfrom
copilot/fix-chunk-size-check

Conversation

Copy link
Contributor

Copilot AI commented Mar 16, 2026

checkChunkSize was called with idx (reqChunks[from]) but validated against a file opened for availableChunks[index]. These differ when the anchor chunk found via binary search isn't the originally requested chunk — causing false size-mismatch errors, particularly for the last chunk which has a smaller expected size.

// Before: idx is the requested chunk, but chunkFile was opened for availableChunks[index]
chunkFile, _ := getSliceChunkFile(c, availableChunks[index])
if err := checkChunkSize(c, chunkFile, idx); err != nil {

// After: pass the actual chunk index matching the opened file
if err := checkChunkSize(c, chunkFile, availableChunks[index]); err != nil {
  • Fix: Pass availableChunks[index] to checkChunkSize to match the chunk file that was actually opened
  • Test: Added Test_getContents_lastChunkSizeMismatch — reproduces the bug using a partial last chunk (100KB vs 512KB block size) where the requested and anchor chunk indices diverge
Original prompt

This section details on the original issue you should resolve

<issue_title>getSliceChunkFile and checkChunkSize may be not match</issue_title>
<issue_description>```
code in getContents like:
fromByte := uint64(reqChunks[from] * uint32(c.md.BlockSize))
if index < len(availableChunks) {
chunkFile, _ := getSliceChunkFile(c, availableChunks[index])
if chunkFile != nil {
if err := checkChunkSize(c, chunkFile, idx); err != nil {
_ = chunkFile.Close()
_ = c.bucket.Discard(context.Background(), c.id)
return nil, 0, err
}

chunkFile, _ := getSliceChunkFile(c, availableChunks[index]
checkChunkSize(c, chunkFile, idx)


the question is here: 
`		chunkFile, _ := getSliceChunkFile(c, availableChunks[index])
		if chunkFile != nil {
			if err := checkChunkSize(c, chunkFile, idx`
idx != availableChunks[index]</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

In getContents, checkChunkSize was called with idx (reqChunks[from])
but the chunk file was opened for availableChunks[index]. These can be
different values, causing incorrect size validation — especially for the
last chunk which has a different (smaller) expected size.

Fixes omalloc/tavern#issue

Co-authored-by: sendya <5404542+sendya@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix mismatch in getSliceChunkFile and checkChunkSize Fix chunk index mismatch between getSliceChunkFile and checkChunkSize Mar 16, 2026
Copilot AI requested a review from sendya March 16, 2026 06:42
@sendya sendya marked this pull request as ready for review March 17, 2026 03:30
Copilot AI review requested due to automatic review settings March 17, 2026 03:30
@sendya sendya merged commit 943cead into main Mar 17, 2026
3 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an incorrect chunk-index being passed to checkChunkSize in the caching middleware, which could trigger false “size mismatch” errors when the anchor chunk found via binary search differs from the originally requested chunk (notably for a partial last chunk).

Changes:

  • Pass availableChunks[index] (the chunk actually opened) into checkChunkSize in getContents.
  • Add a regression test covering the partial last-chunk mismatch scenario.
  • Minor related refactors/adjustments (iobuf helper/parameter rename) and module dependency bumps.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
server/middleware/caching/internal.go Fixes the checkChunkSize index mismatch when using an anchor chunk; adjusts how the combined reader is constructed.
server/middleware/caching/caching_chunkpart_test.go Adds a test reproducing the last-chunk partial size mismatch scenario.
pkg/iobuf/part_reader.go Renames the PartsReadCloser parameter to clarify “optional closer” intent (no functional change).
pkg/iobuf/closer_reader.go Adds NopCloser() helper used by the caching code path.
go.mod Bumps multiple dependency versions.
go.sum Updates checksums corresponding to the dependency bumps.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

// get all hit block
availableChunks := c.getAvailableChunks()
slices.SortFunc(availableChunks, func(i, j uint32) int {
return int(i - j)
Comment on lines 7 to +29
@@ -26,50 +26,50 @@ require (
github.com/shirou/gopsutil/v4 v4.26.1
github.com/stretchr/testify v1.11.1
go.uber.org/zap v1.27.1
golang.org/x/sync v0.18.0
golang.org/x/sync v0.20.0
Comment on lines +14 to +23
type nopCloser struct {
}

func NopCloser() io.Closer {
return &nopCloser{}
}

func (nopCloser) Close() error {
return nil
}
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.

getSliceChunkFile and checkChunkSize may be not match

3 participants