Repository navigation
fix(cache): bound shared chunk download responses - #5261
singh4jitendra wants to merge 1 commit into
Conversation
Copy at most the expected chunk length plus one byte before rejecting an oversized response. Preserve stored gzip range semantics and verify bounded reads, fallback, closure and removal of partial chunks.
There was a problem hiding this comment.
Code Review
This pull request updates the SharedChunkCacheReader to support compressed reads for gzip-encoded objects and bounds the downloaded response size using io.LimitReader to prevent unbounded cache consumption. It also adds unit tests to verify these behaviors. The feedback suggests closing the temporary file before calling os.Remove during cleanup to avoid file-sharing violations on Windows and ensure immediate disk space reclamation.
| if err != nil || bytesWritten != (chunkEnd-chunkStart) { | ||
| os.Remove(tmpPath) // Cleanup |
There was a problem hiding this comment.
On operating systems like Windows, attempting to remove an open file via os.Remove will fail with an access/sharing violation error. Even on Unix-like systems, closing the file descriptor before unlinking ensures that the disk space is reclaimed immediately rather than waiting for the process or deferred function to close it. We should close tmpFile and set it to nil (to prevent the deferred close from running) before calling os.Remove.
if err != nil || bytesWritten != (chunkEnd-chunkStart) {
tmpFile.Close()
tmpFile = nil
os.Remove(tmpPath) // CleanupThere was a problem hiding this comment.
Thanks for looking! This close/unlink ordering is unchanged from master. The deferred close runs as soon as the error path returns, including when an overlong response is rejected, and unlinking an open file is supported on Linux. The new regression checks that no temporary or published chunk remains. I have kept the existing file lifecycle here so this fix stays focused on bounding the response.
|
Hi @vadlakondaswetha, could you help approve the pending CI runs and review this cache download fix when you have a chance? I've replied to the bot's cleanup note as well. Thanks! |
|
Hi @vadlakondaswetha, could you help get the pending CI runs approved and review the bounded cache download change when you have time? Thanks! |
|
Hi @vadlakondaswetha, could you review the cache download fix when you have a chance? Thanks! |
Description
The shared chunk cache checks a download's size only after copying the entire response. An oversized response can therefore consume much more temporary space than the requested chunk.
This caps the copy at the expected length plus one byte, rejects mismatches, and leaves no partial cache files behind. It also requests stored bytes for gzip objects, matching the other readers' range semantics. Cache keys and fallback behavior are unchanged.
This is separate from the cache identity fix in #5225 and filesystem confinement in #5231, though their shared-reader edits may need to be combined when rebasing.
Link to the issue in case of a bug fix.
Fixes #5258
Testing details
Go 1.27.4 on Ubuntu 24.04 under WSL.
make build- passed with0lint issues.go test -mod=readonly -p 1 -count=1 -timeout=90s -run '^TestSharedChunkCache(BoundsDownloadResponse|PreservesStoredGzipBytes)$' ./internal/gcsx- failed as expected before the fix.go test -mod=readonly -p 1 -count=1 -race -timeout=5m ./internal/gcsx ./internal/gcsx/read_manager ./internal/cache/file- passed on native Linux storage.ReadCompressedand returned stored bytes.Any backward incompatible change? If so, please explain.
Oversized responses are rejected earlier and use the existing GCS-reader fallback. Gzip objects are requested without transcoding, consistently with the other readers. Chunk sizes, cache layout, permissions and normal reads are unchanged.