Skip to content

Commit bf34c4f

Browse files
committed
Fix race condition in MemoryCache.Compact()
Fixes #12430 Add ToArray() snapshot before OrderBy to prevent concurrent modification exceptions during enumeration of ConcurrentDictionary.
1 parent 1f0c390 commit bf34c4f

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Utilities/MemoryCache`2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void Set(TKey key, TValue value)
6464

6565
protected virtual void Compact()
6666
{
67-
var kvps = _dict.OrderBy(x => x.Value.LastAccess).ToArray();
67+
var kvps = _dict.ToArray().OrderBy(x => x.Value.LastAccess).ToArray();
6868

6969
for (var i = 0; i < _sizeLimit / 2; i++)
7070
{

0 commit comments

Comments
 (0)