Skip to content

Linked cache entry thread safety fix - #131931

Draft
rosebyte wants to merge 2 commits into
dotnet:mainfrom
rosebyte:rosebyte-issue-46032-analysis
Draft

Linked cache entry thread safety fix#131931
rosebyte wants to merge 2 commits into
dotnet:mainfrom
rosebyte:rosebyte-issue-46032-analysis

Conversation

@rosebyte

@rosebyte rosebyte commented Aug 6, 2026

Copy link
Copy Markdown
Member

Fixes #46032

@azure-pipelines

Copy link
Copy Markdown
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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 ExpirationTokensList backing ICacheEntry.ExpirationTokens to allow lock-free token scans on cache hits.
  • Update CacheEntryTokens to 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 _));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible thread safety issue with linked cache entries

2 participants