Linked cache entry thread safety fix - #131931
Draft
rosebyte wants to merge 2 commits into
Draft
Conversation
added 2 commits
August 6, 2026 08:09
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR changes CacheEntry expiration-token handling to make linked cache entry token propagation safe under concurrent reads by switching from a mutable List<T> to a copy-on-write token list, and adds stress/regression tests to cover the concurrent scenarios.
Changes:
- Introduce a copy-on-write
ExpirationTokensListbackingICacheEntry.ExpirationTokensto allow lock-free token scans on cache hits. - Update
CacheEntryTokensto use the new list, enumerating via snapshots and simplifying token propagation. - Add new tests that exercise concurrent token addition/propagation while entries are being read.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/libraries/Microsoft.Extensions.Caching.Memory/tests/TokenExpirationTests.cs | Adds concurrency + list-semantics tests for expiration tokens. |
| src/libraries/Microsoft.Extensions.Caching.Memory/tests/CacheEntryScopeExpirationTests.cs | Adds a linked-entry concurrency test for propagating child tokens into a committed parent entry. |
| src/libraries/Microsoft.Extensions.Caching.Memory/src/CacheEntry.ExpirationTokensList.cs | Adds a copy-on-write IList<IChangeToken> implementation with snapshot enumeration. |
| src/libraries/Microsoft.Extensions.Caching.Memory/src/CacheEntry.CacheEntryTokens.cs | Switches expiration token storage to the copy-on-write list and updates scanning/propagation logic. |
Comment on lines
+20
to
24
| // ExpirationTokensList is copy-on-write, so CheckForExpiredTokens - which runs on every | ||
| // cache hit - can scan the tokens without taking a lock while another thread adds to them. | ||
| private ExpirationTokensList? _expirationTokens; | ||
| private List<IDisposable>? _expirationTokenRegistrations; | ||
| private List<PostEvictionCallbackRegistration>? _postEvictionCallbacks; // this is not really related to tokens, but was moved here to shrink typical CacheEntry size |
Comment on lines
95
to
101
| internal void PropagateTokens(CacheEntry parentEntry) | ||
| { | ||
| if (_expirationTokens != null) | ||
| ExpirationTokensList? expirationTokens = _expirationTokens; | ||
| if (expirationTokens is not null) | ||
| { | ||
| lock (this) | ||
| { | ||
| CacheEntryTokens parentTokens = parentEntry.GetOrCreateTokens(); | ||
| lock (parentTokens) | ||
| { | ||
| parentTokens.ExpirationTokens.AddRange(_expirationTokens); | ||
| } | ||
| } | ||
| parentEntry.GetOrCreateTokens().ExpirationTokens.AddRange(expirationTokens.Snapshot); | ||
| } |
Comment on lines
+283
to
+287
| // Every read scans the entry's expiration tokens; it must never observe a torn list. | ||
| while (!writer.IsCompleted) | ||
| { | ||
| Assert.True(cache.TryGetValue(key, out _)); | ||
| } |
Comment on lines
+542
to
+546
| Task allWorkers = Task.WhenAll(workers); | ||
| while (!allWorkers.IsCompleted) | ||
| { | ||
| Assert.True(cache.TryGetValue(parentKey, out _)); | ||
| } |
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.
Fixes #46032