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

Fix race in LFU concurrent update and TryRemove(kvp) #618

Merged
merged 1 commit into from
Aug 14, 2024
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: 26 additions & 0 deletions BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuSoakTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,32 @@ await Threaded.Run(threads, i =>
RunIntegrityCheck(cache, this.output);
}

[Fact]
public async Task WhenConcurrentUpdateAndRemoveKvp()
{
var cache = new ConcurrentLfu<int, string>(1, 20, new BackgroundThreadScheduler(), EqualityComparer<int>.Default);
TaskCompletionSource<int> tcs = new TaskCompletionSource<int> ();

var removal = Task.Run(() =>
{
while (!tcs.Task.IsCompleted)
{
cache.TryRemove(new KeyValuePair<int, string>(5, "x"));
}
});

for (var i = 0; i < 100_000; i++)
{
cache.AddOrUpdate(5, "a");
cache.TryGet(5, out _).Should().BeTrue("key 'a' should not be deleted");
cache.AddOrUpdate(5, "x");
}

tcs.SetResult(int.MaxValue);

await removal;
}

private ConcurrentLfu<int, string> CreateWithBackgroundScheduler()
{
var scheduler = new BackgroundThreadScheduler();
Expand Down
43 changes: 26 additions & 17 deletions BitFaster.Caching/Lfu/ConcurrentLfuCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,20 +317,23 @@ public bool TryRemove(KeyValuePair<K, V> item)
{
if (this.dictionary.TryGetValue(item.Key, out var node))
{
if (EqualityComparer<V>.Default.Equals(node.Value, item.Value))
{
var kvp = new KeyValuePair<K, N>(item.Key, node);
lock (node)
{
if (EqualityComparer<V>.Default.Equals(node.Value, item.Value))
{
var kvp = new KeyValuePair<K, N>(item.Key, node);

#if NET6_0_OR_GREATER
if (this.dictionary.TryRemove(kvp))
if (this.dictionary.TryRemove(kvp))
#else
// https://devblogs.microsoft.com/pfxteam/little-known-gems-atomic-conditional-removals-from-concurrentdictionary/
if (((ICollection<KeyValuePair<K, N>>)this.dictionary).Remove(kvp))
// https://devblogs.microsoft.com/pfxteam/little-known-gems-atomic-conditional-removals-from-concurrentdictionary/
if (((ICollection<KeyValuePair<K, N>>)this.dictionary).Remove(kvp))
#endif
{
node.WasRemoved = true;
AfterWrite(node);
return true;
{
node.WasRemoved = true;
AfterWrite(node);
return true;
}
}
}
}
Expand Down Expand Up @@ -361,14 +364,20 @@ public bool TryUpdate(K key, V value)
{
if (this.dictionary.TryGetValue(key, out var node))
{
node.Value = value;
lock (node)
{
if (!node.WasRemoved)
{
node.Value = value;

// It's ok for this to be lossy, since the node is already tracked
// and we will just lose ordering/hit count, but not orphan the node.
this.writeBuffer.TryAdd(node);
TryScheduleDrain();
this.policy.OnWrite(node);
return true;
// It's ok for this to be lossy, since the node is already tracked
// and we will just lose ordering/hit count, but not orphan the node.
this.writeBuffer.TryAdd(node);
TryScheduleDrain();
this.policy.OnWrite(node);
return true;
}
}
}

return false;
Expand Down
Loading