Skip to content

fix(cache): bound shared chunk download responses - #5261

Open
singh4jitendra wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
singh4jitendra:fix/bound-shared-cache-downloads
Open

singh4jitendra wants to merge 1 commit into
GoogleCloudPlatform:masterfrom
singh4jitendra:fix/bound-shared-cache-downloads

Conversation

@singh4jitendra

Copy link
Copy Markdown
Contributor

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.

  1. Manual -
    • make build - passed with 0 lint issues.
  2. Unit tests -
    • 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.
    • A 16-byte request against a 4096-byte fake response now consumes at most 17 bytes and leaves no temporary or published chunks. Another test checks ReadCompressed and returned stored bytes.
  3. Integration tests - Not run against GCS or NFS. Tests use small fake streams only.

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.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines 266 to 267
if err != nil || bytesWritten != (chunkEnd-chunkStart) {
os.Remove(tmpPath) // Cleanup

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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) // Cleanup

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@singh4jitendra
singh4jitendra marked this pull request as ready for review November 17, 2026 05:31
@singh4jitendra
singh4jitendra requested a review from a team as a code owner November 17, 2026 05:31
@singh4jitendra

Copy link
Copy Markdown
Contributor Author

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!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @vadlakondaswetha, could you help get the pending CI runs approved and review the bounded cache download change when you have time? Thanks!

@singh4jitendra

Copy link
Copy Markdown
Contributor Author

Hi @vadlakondaswetha, could you review the cache download fix when you have a chance? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant