Skip to content

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

@Aniketsingh1

Description

@Aniketsingh1

How are you running Flagsmith

  • Self Hosted with Docker
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions