Make MemoryCache thread-safety tests deterministic#129897
Open
sami-daniel wants to merge 2 commits into
Open
Conversation
The three concurrency tests in MemoryCacheSetAndRemoveTests relied on fixed multi-second delays plus task cancellation to bound their run, which made them slow and racy, so they were disabled. Rewrite them to run a fixed number of iterations per thread, started together with a Barrier, removing all wall-clock timing. OvercapacityPurge_AreThreadSafe also created its tasks with a cancellation token, so cancellation left them Canceled and Task.WaitAll threw TaskCanceledException. AddAndReplaceEntries_AreThreadSafe shared a single Random across threads, which is not thread-safe; each thread now owns its own instance. Re-enable both. GetAndSet_AreThreadSafe_AndUpdatesNeverLeavesNullValues is de-raced too but stays disabled under dotnet#72879, an unrelated cache bug it exercises. Fix dotnet#72890
Contributor
|
Tagging subscribers to this area: @dotnet/area-extensions-caching |
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.
The three concurrency tests in
MemoryCacheSetAndRemoveTestswere disabled because they were timing-sensitive and racy. They relied on fixed multi-second delays to bound their run, so every run took ~7s and the outcome depended on thread scheduling. Two of them also had bugs in the test code itself:OvercapacityPurge_AreThreadSafecreated its worker tasks with a cancellation token (Task.Run(..., cts.Token)). When the token fired, the tasks ended up Canceled, soTask.WaitAllthrewTaskCanceledException, the failure tracked in the issue.AddAndReplaceEntries_AreThreadSafeshared a single Random instance across three threads. Random is not thread-safe, so concurrent use corrupted its state.Fix #72890