I was investigating the code of TryRemove(KeyValuePair<K, V>) and found a race condition.
Under certain circumstances the item is removed even when value doesn't match.
Here is the reproduction code:
using BitFaster.Caching;
using BitFaster.Caching.Lru;
var cache = (ICacheExt<int, string>)new ConcurrentLru<int, string>(capacity: 100);
Task.Run(() =>
{
for (;;)
{
cache.TryRemove(new KeyValuePair<int, string>(5, "x"));
}
});
for (var i = 0; i < int.MaxValue; i++)
{
cache.AddOrUpdate(5, "a");
if (!cache.TryGet(5, out _))
{
throw new Exception("My value is gone :-("); // This should not happen, we are only removing (5, "x")
}
cache.AddOrUpdate(5, "x");
}
I believe this shouldn't be expected. I kindly ask for confirmation.