Skip to content

fix(cache): skip archiving in saveCacheV2 when the key already exists - #2556

Open
trivikr wants to merge 2 commits into
actions:mainfrom
trivikr:cache-key-lookup-before-tar
Open

trivikr wants to merge 2 commits into
actions:mainfrom
trivikr:cache-key-lookup-before-tar

Conversation

@trivikr

@trivikr trivikr commented Oct 25, 2026

Copy link
Copy Markdown

Fixes: #2550

saveCacheV2 created the archive before reserving the cache key. When another job had already saved the key (common with matrix jobs sharing a key), every later job compressed the full cache, then failed with Unable to reserve cache with key <key>, another job may be creating this cache.

Changes

  • Before creating the archive, look up the exact key and version with GetCacheEntryDownloadURL. If an entry exists, log Cache entry with key <key> already exists, not saving cache. and return -1 without archiving or reserving.
  • The lookup is best-effort and makes a single attempt (maxAttempts: 1). On failure the save proceeds as before.
  • A save still in progress in another job is not detected and fails at reservation, as it does today.
  • saveCacheV1 is unchanged.

saveCacheV2 created the archive before reserving the cache key. When
another job had already saved the key, the archive was built and then
discarded, and the save failed with ReserveCacheError.

Look up the exact key and version with GetCacheEntryDownloadURL before
creating the archive, and return -1 if an entry already exists. The
lookup is best-effort and makes a single attempt; on failure the save
proceeds as before. A save still in progress in another job is not
detected and fails at reservation as it does today.
@trivikr
trivikr requested a review from a team as a code owner October 25, 2026 23:05
Copilot AI balanced review requested due to automatic review settings October 25, 2026 23:05

Copilot AI 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.

🟡 Changes recommended

The preflight can suppress valid branch-local saves and attempts reads in write-only mode.

2 open issues
What changed in this PR

This PR addresses #2550 by adding a best-effort lookup before saveCacheV2 creates an archive, aiming to avoid redundant compression when a cache already exists.

Changes:

  • Skip archiving and reservation on an exact key/version lookup hit.
  • Use a single lookup attempt and continue saving if it fails.
  • Add tests for existing entries, different-key matches, and lookup failures; leave saveCacheV1 unchanged.
File Description
packages/​cache/​src/​cache.ts Adds the pre-archive lookup and early return.
packages/​cache/​__tests__/​saveCacheV2.test.ts Tests skip behavior and fallback save paths.

Review effort: Balanced

Comment thread packages/cache/src/cache.ts Outdated
Comment on lines +681 to +683
if (existing.ok && existing.matchedKey === key) {
core.info(`Cache entry with key ${key} already exists, not saving cache.`)
return cacheId

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Correct that the lookup searches all readable scopes, and the response doesn't say which one matched. I think skipping is still right: restore on this branch would already return that default-branch entry for the same key and version, so a branch copy adds nothing for reads. It's the same rule actions/cache applies when restore hits the primary key ("Cache hit occurred on the primary key, not saving cache"), which can also come from the default branch. This applies it at save time. A cross-branch test can't exercise scope client-side beyond mocking matchedKey, which the existing test does. If maintainers want branch-local copies kept, I can make this opt-in via UploadOptions.

Comment thread packages/cache/src/cache.ts Resolved
The preflight lookup in saveCacheV2 is a read, but ACTIONS_CACHE_MODE
write-only does not permit reads, and restoreCache already skips them
in that mode. Only run the lookup when isCacheReadable(getCacheMode())
is true, so write-only saves go straight to archiving and reservation.

This branch has not been deployed

No deployments
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.

Reserve cache key before creating the archive in saveCacheV2

2 participants