Repository navigation
fix(http-cache): a late single-flight leader re-reads the entry before fetching - #646
Merged
Merged
Conversation
…e fetching A resolve that missed the cache before the leader stored, then reached single-flight after the leader left, became a second leader and fetched a URL that was already cached and fresh. The new leader now re-reads the entry under its own flight registration (ordered after the previous leader's store) and serves it when fresh; otherwise that newer snapshot, not the caller's, drives the fetch. Adds LateArrivalAfterTheLeaderLeftIsServedFromTheCache, which failed 13 of 20 runs without the fix and passes 30 of 30 with it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
HttpCacheTest.SingleFlightCoalescesConcurrentResolvesfailed on #645's ubuntu Debug run (fake->callswas 2, expected 1). The cache had a real race, not a slow runner:fetch_and_cache, finds no marker and becomes a second leader. Its snapshot from step 1 predates the store, so it fetches again.The result is a duplicate transfer of a URL that is already cached and fresh. That's wasted bandwidth, and it's the bug single-flight exists to prevent.
Change
fetch_and_cachere-reads the entry after it registers as leader. The previous leader stored before it erased its marker under the flight lock, and the new leader registers under that same lock, so this read sees the store. A fresh entry is served with no transfer.fetch_and_cacheno longer takes the caller's snapshot.Test
LateArrivalAfterTheLeaderLeftIsServedFromTheCache: 1000 rounds; each round starts 8 threads together on one key against an instant transport, which makes the late-arrival window common. Each round must be exactly one transfer.Measured locally (Windows, MSVC Release):
SingleFlightCoalescesConcurrentResolvespassed 30 of 30 runs.uri_http_cache_testpass in 1.9 s.