Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions server/middleware/caching/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func getContents(c *Caching, reqChunks []uint32, from uint32) (io.ReadCloser, in
if err := checkChunkSize(c, f, idx); err == nil {
return f, 1, nil
}
_ = f.Close()
}

// find all hit block.
Expand All @@ -192,6 +193,7 @@ func getContents(c *Caching, reqChunks []uint32, from uint32) (io.ReadCloser, in
chunkFile, _ := getSliceChunkFile(c, availableChunks[index])
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

The error return from getSliceChunkFile is ignored here (chunkFile, _ := ...). getSliceChunkFile can return non-NotExist errors (including EMFILE via isTooManyFiles), and ignoring it will silently fall back to an upstream fill instead of failing fast. Handle and return the error (similar to the earlier f, err1 := ... branch).

Suggested change
chunkFile, _ := getSliceChunkFile(c, availableChunks[index])
chunkFile, err2 := getSliceChunkFile(c, availableChunks[index])
if err2 != nil {
return nil, 0, err2
}

Copilot uses AI. Check for mistakes.
if chunkFile != nil {
if err := checkChunkSize(c, chunkFile, idx); err != nil {
_ = chunkFile.Close()
_ = c.bucket.Discard(context.Background(), c.id)
return nil, 0, err
}
Comment on lines 195 to 199
Copy link

Copilot AI Mar 6, 2026

Choose a reason for hiding this comment

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

checkChunkSize should be called with the chunk index that corresponds to the file being validated. Here chunkFile is opened for availableChunks[index], but idx (the requested starting chunk) is passed instead. This can incorrectly fail validation (notably when the anchor chunk is the last, smaller chunk) and trigger an unnecessary discard. Pass availableChunks[index] to checkChunkSize here.

Copilot uses AI. Check for mistakes.
Expand All @@ -202,6 +204,7 @@ func getContents(c *Caching, reqChunks []uint32, from uint32) (io.ReadCloser, in
// Request is automatically cloned by getUpstreamReader
reader, err := c.getUpstreamReader(fromByte, toByte, true)
if err != nil {
_ = chunkFile.Close()
return nil, 0, err
}

Expand Down