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

HybridCache: Inconsistent behavior of GetOrCreateAsync in concurrent calls with readonly flags set #6202

Open
1 task done
bezaou opened this issue Mar 26, 2025 · 0 comments

Comments

@bezaou
Copy link

bezaou commented Mar 26, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using the GetOrCreateAsync method in a concurrent scenario with the DisableLocalCacheWrite and DisableDistributedCacheWrite flags set, the factory method is sometimes not called.

This leads to inconsistent behavior when attempting to check if a specific key exists in the cache or not.

Expected Behavior

The factory method should always be invoked when the key does not exist in the cache.

Steps To Reproduce

        var tasks = new List<Task<(bool, bool)>>();
        for (var i = 0; i < 100; i++)
        {
            tasks.Add(Task.Run(async () =>
            {
                var exist = true;

                var result = await _cacheService.GetOrCreateAsync<object, bool>(
                    "test_key",
                    null!,
                    (_, _) =>
                    {
                        exist = false;
                        return new ValueTask<bool>(default(bool)!);
                    },
                    new HybridCacheEntryOptions
                    {
                        Flags = HybridCacheEntryFlags.DisableLocalCacheWrite | HybridCacheEntryFlags.DisableDistributedCacheWrite
                    });

                return (exist, result!);
            }));
        }

        var resultsArray = await Task.WhenAll(tasks);
        var results = resultsArray.ToList();

Image

Exceptions (if any)

No response

.NET Version

dotnet 9

Anything else?

Microsoft.Extensions.Caching.Hybrid with version 9.3.0
Microsoft.Extensions.Caching.StackExchangeRedis with version 9.0.2

@bezaou bezaou changed the title HybridCache: Inconsistent Behavior of GetOrCreateAsync in Concurrent Calls with readonly Flags Set HybridCache: Inconsistent behavior of GetOrCreateAsync in concurrent calls with readonly flags set Mar 26, 2025
@BrennanConroy BrennanConroy transferred this issue from dotnet/aspnetcore Mar 26, 2025
@github-actions github-actions bot added the area-ai Microsoft.Extensions.AI libraries label Mar 26, 2025
@jeffhandley jeffhandley added area-caching-hybrid and removed area-ai Microsoft.Extensions.AI libraries labels Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants