[6.2] Cherry Pick - Fix inverted logic on registry cache expiration #9144 #9210
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Cherry pick of #9144
In
RegistryClient.swift
there is an in-memory package metadata cache expiration checkThe logic for
cached.expires < .now()
appears to be inverted, e.g. if a cache is found for the cacheKey, the initial values look like this2389992062152
<2303592072235
which equates to false, becausecached.expires
is set with a TTL of 60*60 seconds in the future. This leads to the cache only being used after it has expired and may be causing downstream fingerprint TOFU issues when new package releases are published and the metadata cache is in-memory.Motivation:
This issue was identified via the use of a private SPM registry conforming to the registry server specification. Disclaimer: This issue was also occurring in previous versions of Xcode but now that they are sharing code with SPM, I hope this example is still relevant. The scenario it is occurring in is as follows:
swift package resolve
via CLI at this point results the following error:invalid registry source archive checksum 'newchecksum', expected 'previouschecksum'
Because the checksum fingerprint is stored on disk in
~/Library/org.swift.swiftpm/security/fingerprints/
, it takes manual intervention to recover from this by purging various caches.Example of a common workaround: https://stackoverflow.com/questions/79417322/invalid-registry-source-archive-checksum-a-expected-b
Related github issue: #8981
In addition to that, there are probably excessive requests being sent to various registries due to the cache not being used during the initial TTL.
Modifications:
Two main changes in this PR that should help with this:
version
property to theMetadataCacheKey
, so that even if the previous metadata is in cache and hasn't expired, it won't be applied to the new version.In addition to that, this PR also:
availabilityCache
withAvailabilityCheck
to internal (from private) for testability.Result:
The hopeful result of this is that package registry consumers will no longer require manual workarounds to fix their local state when they hit this edge case, as well as avoid hitting github and other package registries too often (my guess for the original intent of the cache).
That said, it will be a bit tricky to validate in its entirety because this cache is only relevant for long-running tasks via libSwiftPM I presume.
One big thing to note is that this may be a noticeable change to existing behavior since the cache is not currently being used as far as I can tell, and once fixed, it will significantly reduce the freshness of the metadata compared to before.