Skip to content

TryRemove(KeyValuePair<K, V>) removes item even when value doesn't match #615

@mobratil

Description

@mobratil

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions