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

PostEvictionCallbacks does not get invoked when MemoryCache entries expire with an active change token #46774

Closed
pranavkm opened this issue Jan 8, 2021 · 3 comments · Fixed by #46807

Comments

@pranavkm
Copy link
Contributor

pranavkm commented Jan 8, 2021

Observed as part of a runtime update: dotnet/aspnetcore#29153

Scenario:

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;

namespace cache_repro
{
    class Program
    {
        static bool _evicted;

        static async Task Main(string[] args)
        {
            var cache = new MemoryCache(new MemoryCacheOptions());
            var key = new object();

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            cache.Set(key, new object(), new MemoryCacheEntryOptions
            {
                ExpirationTokens = { new Microsoft.Extensions.Primitives.CancellationChangeToken(cts.Token) },
                PostEvictionCallbacks = { new PostEvictionCallbackRegistration { EvictionCallback = OnEntryEvicted } },
            });

            System.Console.WriteLine(cache.TryGetValue(key, out _));

            await Task.Delay(TimeSpan.FromSeconds(5));
            System.Console.WriteLine(cache.TryGetValue(key, out _));
            System.Console.WriteLine($"Evicted: {_evicted}");
        }

        static void OnEntryEvicted(object key, object value, EvictionReason reason, object state)
        {
            _evicted = true;
        }
    }
}

Using 5.0.0 version of Microsoft.Extensions.Caching.Memory, the post eviction callback is invoked when the CTS expires and causes the cache entry to be evicted. Using 6.0.0-alpha.1.21057.11, the callback no longer get invoked.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jan 8, 2021
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Jan 8, 2021

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.

Issue Details

Observed as part of a runtime update: dotnet/aspnetcore#29153

Scenario:

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;

namespace cache_repro
{
    class Program
    {
        static bool _evicted;

        static async Task Main(string[] args)
        {
            var cache = new MemoryCache(new MemoryCacheOptions());
            var key = new object();

            var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));

            cache.Set(key, new object(), new MemoryCacheEntryOptions
            {
                ExpirationTokens = { new Microsoft.Extensions.Primitives.CancellationChangeToken(cts.Token) },
                PostEvictionCallbacks = { new PostEvictionCallbackRegistration { EvictionCallback = OnEntryEvicted } },
            });

            System.Console.WriteLine(cache.TryGetValue(key, out _));

            await Task.Delay(TimeSpan.FromSeconds(5));
            System.Console.WriteLine(cache.TryGetValue(key, out _));
            System.Console.WriteLine($"Evicted: {_evicted}");
        }

        static void OnEntryEvicted(object key, object value, EvictionReason reason, object state)
        {
            _evicted = true;
        }
    }
}

Using 5.0.0 version of Microsoft.Extensions.Caching.Memory, the post eviction callback is invoked when the CTS expires and causes the cache entry to be evicted. Using 6.0.0-alpha.1.21057.11, the callback no longer get invoked.

Author: pranavkm
Assignees: -
Labels:

area-Extensions-Caching, untriaged

Milestone: -

@eerhardt
Copy link
Member

eerhardt commented Jan 8, 2021

@adamsitnik - it sounds like this is probably caused from 533a807. Can you look into it?

@adamsitnik adamsitnik self-assigned this Jan 11, 2021
@adamsitnik adamsitnik added bug and removed untriaged New issue has not been triaged by the area owner labels Jan 11, 2021
@adamsitnik adamsitnik added this to the 6.0.0 milestone Jan 11, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 11, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 11, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Feb 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants