Fix race condition in MemoryCache.Compact() enumeration #12433
Closed
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.
ConcurrentDictionaryenumeration throws when the dictionary is modified during iteration. TheCompact()method was enumerating with LINQ'sOrderBy()while concurrentSet()calls could modify the dictionary, causing intermittentArgumentExceptionin CI.Changes
Snapshot the dictionary before ordering to isolate the LINQ enumeration from concurrent modifications:
The initial
ToArray()creates a point-in-time snapshot of the dictionary entries, preventing the enumeration exception while maintaining intended compaction behavior.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
2kmvsblobprodcus39.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)2zrvsblobprodcus388.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)7devsblobprodcus323.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)7tjvsblobprodcus341.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)bcnvsblobprodcus378.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)frdvsblobprodcus327.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)ibzvsblobprodcus369.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)imzvsblobprodcus368.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)josvsblobprodcus372.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)kgfvsblobprodcus314.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)kh4vsblobprodcus325.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)kijvsblobprodcus387.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)l49vsblobprodcus358.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)s4uvsblobprodcus326.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)u3hvsblobprodcus371.vsblob.vsassets.iodotnet build src/Razor/test/Microsoft.CodeAnalysis.Razor.Workspaces.Test/Microsoft.CodeAnalysis.Razor.Workspaces.Test.csproj(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>Race Condition in MemoryCache.Compact() Causes Intermittent Test Failures</issue_title>
<issue_description># Race Condition in MemoryCache.Compact() Causes Intermittent Test Failures
Summary
The
MemoryCache<TKey, TValue>.Compact()method has a race condition that causes intermittent test failures when the cache is accessed concurrently. The method enumerates aConcurrentDictionarywhile it can be modified by other threads, violating thread-safety guarantees.Environment
Steps to Reproduce
./restore.sh./build.sh -testMicrosoft.CodeAnalysis.Razor.Workspaces.TestNote: This is a race condition, so it may not reproduce consistently.
Expected Behavior
The
ConcurrentSets_DoesNotThrowtest should pass consistently.Actual Behavior
Test intermittently fails with:
Stack Trace
Test Results
Before fix:
After fix:
Root Cause
In
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Utilities/MemoryCache2.csat line 67, theCompact()method enumerates theConcurrentDictionary` with LINQ:The problem:
Set()concurrentlySet()can triggerCompact()when the cache is fullCompact()enumerates theConcurrentDictionarywith.OrderBy().ToArray()Set()ArgumentExceptionWhile
ConcurrentDictionaryis thread-safe for individual operations, enumerating it while it's being modified can throw exceptions. This is documented behavior ofConcurrentDictionary.Solution
Snapshot the dictionary before ordering to prevent enumeration exceptions:
Fix
File:
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Utilities/MemoryCache2.cs`Line: 67
Before:
After:
The initial
.ToArray()creates a snapshot of the dictionary at that moment, preventing concurrent modifications from affecting the LINQ enumeration.Additional Context
ConcurrentSets_DoesNotThrowis specifically designed to test concurrent accessImpact
References
Microsoft.CodeAnalysis.Razor.Utilities.MemoryCacheTest.ConcurrentSets_DoesNotThrow</issue_description>
Comments on the Issue (you are @copilot in this section)
@davidwengier This one fails on my machine regularly, but it's easy enough to ignore. Interestingly I've never seen it fail in CI on our Ubuntu legs.I presume this was gener...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.