Skip to content

Fix flaky tests caused by data races in MockDeviceCache#6888

Merged
rickvdl merged 7 commits into
mainfrom
rickvdl/fix-flaky-mockdevicecache-race
Jun 5, 2026
Merged

Fix flaky tests caused by data races in MockDeviceCache#6888
rickvdl merged 7 commits into
mainfrom
rickvdl/fix-flaky-mockdevicecache-race

Conversation

@rickvdl

@rickvdl rickvdl commented Jun 2, 2026

Copy link
Copy Markdown
Member
  • Fixes data races caused by MockDeviceCache possibly being accessed from multiple threads at the same time in tests. This was fixed by backing the fields with Atomic<T> and updating the relevant callsites to use the .modify {} method.
  • Increased timeout on testPurchaseOutsideTheAppUpdatesCustomerInfoDelegate in an attempt to reduce flakiness there as well.

Note

Low Risk
Test-only mock changes with no production SDK behavior; aligns with existing Atomic usage in other mocks.

Overview
Makes customer info tracking in MockDeviceCache thread-safe so unit tests match concurrent SDK access (e.g. configure + foreground sync).

Counters (cacheCustomerInfoCount, cachedCustomerInfoCount, etc.) and the cachedCustomerInfo dictionary are backed by private Atomic storage with the same public property names. Overrides use modify for increments and dictionary updates instead of non-atomic += and subscript writes.

Reviewed by Cursor Bugbot for commit 0a8e91f. Bugbot is set up for automated code reviews on this repo. Configure here.

@rickvdl rickvdl requested a review from a team as a code owner June 2, 2026 14:24
@rickvdl

rickvdl commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

@RCGitBot please test

@rickvdl rickvdl marked this pull request as draft June 2, 2026 14:25

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0a8e91f. Configure here.

var cachedCustomerInfo: [String: Data] {
get { self._cachedCustomerInfo.value }
set { self._cachedCustomerInfo.value = newValue }
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Non-atomic read-modify-write on cachedCustomerInfo via computed property

Low Severity

Converting cachedCustomerInfo from a stored property to a computed property backed by Atomic changes the semantics of mutating calls like cachedCustomerInfo.removeValue(forKey:) (line 257 in clearCustomerInfoCache). This now performs a non-atomic get-copy-mutate-set through the computed property's getter and setter, while cache(customerInfo:appUserID:) correctly uses _cachedCustomerInfo.modify { ... } for an atomic update. If both methods run concurrently, the removeValue can silently lose a concurrent write from cache.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 0a8e91f. Configure here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We should be using the _ cachedCustomerInfo.modify {} in the relevant tests that were causing issues due to multi-thread access, so we should be covered.

We could choose to update all callsites, but I tried to keep the diff minimal.

@rickvdl

rickvdl commented Jun 2, 2026

Copy link
Copy Markdown
Member Author

@RCGitBot please test

@rickvdl rickvdl force-pushed the rickvdl/fix-flaky-mockdevicecache-race branch from 22a155f to 1fdd4e0 Compare June 4, 2026 05:36
@rickvdl

rickvdl commented Jun 4, 2026

Copy link
Copy Markdown
Member Author

@RCGitBot please test

1 similar comment
@rickvdl

rickvdl commented Jun 4, 2026

Copy link
Copy Markdown
Member Author

@RCGitBot please test

@rickvdl rickvdl changed the title Fix data race in MockDeviceCache customer info state Fix flaky tests caused by data races in MockDeviceCache Jun 4, 2026
@rickvdl rickvdl force-pushed the rickvdl/fix-flaky-mockdevicecache-race branch from b05effa to c91390d Compare June 4, 2026 13:54
@rickvdl

rickvdl commented Jun 4, 2026

Copy link
Copy Markdown
Member Author

@RCGitBot please test

@rickvdl rickvdl marked this pull request as ready for review June 4, 2026 14:05

@ajpallares ajpallares left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for this improvement!

@rickvdl rickvdl merged commit 6cdf029 into main Jun 5, 2026
42 checks passed
@rickvdl rickvdl deleted the rickvdl/fix-flaky-mockdevicecache-race branch June 5, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants