Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lock to prevent concurrency issue #2215

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/Sarif/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,23 @@ public TValue this[TKey key]
{
TValue value = default;

if (!_cache.TryGetValue(key, out value))
lock (_keysInUseOrder)
{
// Build and add the new item to cache
value = _builder(key);
if (!_cache.TryGetValue(key, out value))
{
// Build and add the new item to cache
value = _builder(key);

SetValue(key, value);
}
else
{
// When an in-cache key is retrieved, move it back to the start of the order used set, if not already most recent
if (Capacity > 0 && key.CompareTo(_keysInUseOrder.First.Value) != 0)
SetValue(key, value);
}
else
{
_keysInUseOrder.Remove(key);
_keysInUseOrder.AddFirst(key);
// When an in-cache key is retrieved, move it back to the start of the order used set, if not already most recent
if (Capacity > 0 && key.CompareTo(_keysInUseOrder.First.Value) != 0)
{
_keysInUseOrder.Remove(key);
_keysInUseOrder.AddFirst(key);
}
}
}

Expand All @@ -84,6 +87,7 @@ private void SetValue(TKey key, TValue value)
if (Capacity > 0 && _cache.Count >= Capacity)
{
TKey oldest = _keysInUseOrder.Last.Value;

_keysInUseOrder.RemoveLast();
_cache.TryRemove(oldest, out _);
}
Expand Down
21 changes: 21 additions & 0 deletions src/Test.UnitTests.Sarif/FileRegionsCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;

using FluentAssertions;

Expand Down Expand Up @@ -464,6 +466,25 @@ public void FileRegionsCache_PopulatesCarriageReturnFileRegions()
ExecuteTests(COMPLETE_FILE_CARRIAGE_RETURNS_ONLY, s_carriageReturnTestCasess);
}

[Fact]
public void FileRegionsCache_ValidateConcurrencyData()
{
Exception exception = Record.Exception(() =>
{
var fileRegionsCache = new FileRegionsCache();
List<Task> taskList = new List<Task>();

for (int i = 0; i < 1_000; i++)
{
taskList.Add(Task.Factory.StartNew(() =>
fileRegionsCache.PopulateTextRegionProperties(new Region { }, new Uri($"file:///c:/{Guid.NewGuid()}.txt"), true)));
}

Task.WaitAll(taskList.ToArray());
});
Assert.Null(exception);
}

private static void ExecuteTests(string fileText, ReadOnlyCollection<TestCaseData> testCases)
{
Uri uri = new Uri(@"c:\temp\myFile.cpp");
Expand Down