Drop stale cache write-backs from a fetch that began before an identity change#3563
Closed
vegaro wants to merge 7 commits into
Closed
Drop stale cache write-backs from a fetch that began before an identity change#3563vegaro wants to merge 7 commits into
vegaro wants to merge 7 commits into
Conversation
Adds a monotonic cacheGeneration counter to WorkflowsCache, bumped inside the @synchronized clearCache(). Write methods (cacheWorkflow, cacheWorkflowsList, cacheWorkflowsListInMemory, cacheWorkflowDetailEnvelope) now take an expectedGeneration parameter and silently drop the write when the generation has advanced, preventing an in-flight fetch from repopulating the cache after an identity transition clears it. WorkflowManager call sites updated with workflowsCache.currentGeneration() as a temporary compile-fix; Task 2 will replace these with a generation captured at fetch start for true race-condition safety. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The default `expectedGeneration = workflowsCache.currentGeneration()` was
evaluated by the synthesized getWorkflow$default on the caller side, so an
`every {}` block recording getWorkflow on a mocked WorkflowManager (whose
workflowsCache field is null) threw an NPE. Default to null and resolve the
generation inside the method body instead, preserving capture-at-entry for
on-demand fetches.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…traint Adds a stale-generation test for cacheWorkflowsListInMemory (the load-bearing disk-restore write path) and tests pinning the spec non-goal that forceWorkflowsListCacheStale, clearInMemoryOfferingsCache, and forceCacheStale do not bump the generation. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…and-plan # Conflicts: # purchases/src/main/kotlin/com/revenuecat/purchases/common/workflows/WorkflowManager.kt # purchases/src/test/java/com/revenuecat/purchases/common/workflows/WorkflowsCacheTest.kt
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3563 +/- ##
==========================================
+ Coverage 80.19% 80.23% +0.04%
==========================================
Files 371 371
Lines 15239 15271 +32
Branches 2112 2118 +6
==========================================
+ Hits 12221 12253 +32
Misses 2168 2168
Partials 850 850 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Summary
WorkflowsCacheandOfferingsCacheare user-agnostic — an identity transition wipes them withclearCache(). But an in-flight backend fetch can write back after the clear, repopulating the just-cleared cache with the previous user's data, which is then served until the next fetch self-heals it:@Synchronizedcouldn't close this: it serializes individual mutations but can't order aclear()against a write-back that crosses the async backend boundary.This adds a clear-generation token to both caches. Each owns a monotonic
cacheGeneration: Int, bumped inside its@Synchronized clearCache(). A fetch capturescurrentGeneration()at its start and threads that value through the whole async sequence; every write commits only if the captured generation still matches — checked under the same lock the increment uses, so clear-then-write is atomic. A write from any prior epoch is dropped, and the cleared cache stays empty until the next fetch repopulates it for the current user.Both caches are covered together: a workflows-only guard would leave offerings (the thing workflows are keyed to) still racy. Chosen over an appUserID compare because a monotonic counter is correct under fast A→B→A login churn, where the id matches again but the epoch differs.
What changed
WorkflowsCachecacheGeneration+currentGeneration(); bump inclearCache();expectedGenerationguard oncacheWorkflow,cacheWorkflowsList,cacheWorkflowsListInMemory,cacheWorkflowDetailEnvelopeWorkflowManagergetWorkflowsList(threaded through the success write, prefetch loop, and error-restore paths) andgetWorkflow(captured at entry; prefetch passes the list's epoch)OfferingsCachecacheGeneration+currentGeneration(); bump inclearCache();expectedGenerationguard oncacheOfferingsOfferingsManagerfetchAndCacheOfferings, threaded throughcreateAndCacheOfferings(network-success and disk-fallback paths)Out of scope by design:
clearInMemoryOfferingsCache()/forceCacheStale()/forceWorkflowsListCacheStale()do not bump the generation (locale-override and forced-staleness have separate semantics). NoappUserIDis introduced into either cache. All new symbols areinternal— no public API change.Test plan
WorkflowsCacheTest/OfferingsCacheTest:currentGeneration()increments onclearCache(); each gated write is a no-op on a stale generation and commits on the current one; the non-identity-transition clear methods leave the generation unchanged.WorkflowManagerTest/OfferingsManagerTest: the real race against a real cache — capture the success callback,clearCache(), then fire the callback; assert the cleared cache stays empty and disk is not rewritten.:purchasesunit suite (3223 tests),detektAll,api-check— all green, no public API diff.🤖 Generated with Claude Code