flagsmith-dotnet-client icon indicating copy to clipboard operation
flagsmith-dotnet-client copied to clipboard

Deadlock occurs for flag value fetching while using in memory or redis cache for flags

Open Aniketsingh1 opened this issue 2 months ago • 4 comments

How are you running Flagsmith

  • [ ] Self Hosted with Docker
  • [x] Self Hosted with Kubernetes
  • [ ] SaaS at flagsmith.com
  • [ ] Some other way (add details in description below)

Describe the bug

I have added in memory or redis caching for flag values to control flagsmith API hits. But as soon as I add that the flag fetching for the first time becomes a never returning call and its like deadlock occurs.

Here is the code . Do note that I am using the open feature sdk here for flagsmith provider.

Steps To Reproduce

// At the class level
private static readonly ConcurrentDictionary<string, (bool Value, DateTime Timestamp)> featureCache
     = new ConcurrentDictionary<string, (bool Value, DateTime Timestamp)>();`
   public static async Task<bool> GetFeatureState(string flagName, bool defaultValue, OpenFeature.Model.EvaluationContext context, string identity)
   {
       try
       {
           var CommonConfigValue = ConfigurationManager.AppSettings[flagName];
           var flagStatus = false;

           if (string.IsNullOrEmpty(CommonConfigValue) &&
               AppSettings._OpenFeatureProviderName.ToLower() == ProviderEnum.FlagSmith.ToString().ToLower())
           {
               var cacheflagName = $"{identity}:{flagName}";

               if (featureCache.TryGetValue(cacheflagName, out (bool Value, DateTime Timestamp) cachedEntry))
               {
                   SystemLogger.WriteInformation($"Found cached value for {cacheflagName}");
                   // Use cached value only if timestamp is within 30 minutes
                   if ((DateTime.Now - cachedEntry.Timestamp).TotalMinutes < 30)
                   {
                       SystemLogger.WriteInformation($"Using cached value for {cacheflagName}");
                       return cachedEntry.Value;
                   }
               }

               // Retrieve the Flagsmith client from the Application state
               var flagsmithClient = OpenFeature.Api.Instance.GetClient("FsProvider");

               // Check the feature flag asynchronously
               flagStatus = await flagsmithClient.GetBooleanValueAsync(flagName, defaultValue, context)
                                                 .ConfigureAwait(false);

               // Store value with timestamp
               featureCache[cacheflagName] = (flagStatus, DateTime.Now);
           }
           else
           {
               flagStatus = CommonConfigValue?.Trim().ToLower() == "true";
           }

           return flagStatus;
       }
       catch (Exception ex)
       {
           SystemLogger.WriteError($"Error checking feature state: {ex.Message}");
           return false;
       }
   }

Expected behavior

Flag value must be returned and stored in cache for 30 min time

Screenshots

No response

Aniketsingh1 avatar Oct 08 '25 12:10 Aniketsingh1

This is happening inside dot net 4.8

Aniketsingh1 avatar Oct 08 '25 12:10 Aniketsingh1

Any update on this ?

Aniketsingh1 avatar Oct 13 '25 06:10 Aniketsingh1

@Aniketsingh1 I'm sorry, I don't quite follow this issue. It looks like you have built your own caching implementation so I'm not sure how this relates to our SDK? Could you provide any more information?

matthewelwell avatar Oct 23 '25 10:10 matthewelwell

I am trying to cache the flag and its values. Caching mechanism is mine but the call is made to flagsmith to get the values is actually going into a deadlock state and not returning anything.

Aniketsingh1 avatar Oct 28 '25 11:10 Aniketsingh1