Flagsmith / Flagsmith/flagsmith-dotnet-client

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

Open
#169 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
20
Forks
15
PR merge metrics
No merged PRs in 30d

Description

### 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

```csharp
// At the class level
private static readonly ConcurrentDictionary featureCache
= new ConcurrentDictionary();`
```

```csharp
public static async Task 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_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.