Skip to content

HybridCache: tags parsing issue breaks remove by tags behavior #7342

@Oleg52

Description

@Oleg52

Description

I see Cache backend data rejected: ParseFault. warning when calling GetOrCreateAsync without providing tags. I'm passing a key that i had previously added with 2 or more tags. I'm using Redis as a backend cache.

It results in GetOrCreateAsync method returning old data even after calling RemoveByTagAsync.

Reproduction Steps

  1. Create web application with target framework .net10.
  2. Add Microsoft.Extensions.Caching.Hybrid nuget package with version 10.0.0
  3. Add Microsoft.Extensions.Caching.StackExchangeRedis nuget package with version 10.0.0
  4. Update Program.cs with the following reproduction code:
using Microsoft.Extensions.Caching.Hybrid;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = "127.0.0.1:6379"; });
builder.Services.AddHybridCache();

var app = builder.Build();

app.MapGet("/tags", async (HybridCache hybridCache) =>
{
    string[] tags = ["tag1", "tag2"];
    return await hybridCache.GetOrCreateAsync("key", _ => ValueTask.FromResult("data"), tags: tags);
});

app.MapGet("/no-tags", async (HybridCache hybridCache) =>
{
    _ = await hybridCache.GetOrCreateAsync("key", _ => ValueTask.FromResult("data"));
    await hybridCache.RemoveByTagAsync("tag1");
    return await hybridCache.GetOrCreateAsync("key", _ => ValueTask.FromResult("new data"));
});

app.Run();
  1. Run app and send GET request on /tags.
  2. Re-run app and send GET request on /no-tags.

Expected behavior

  1. No warning is shown.
  2. /tags endpoint returns data.
  3. /no-tags endpoint returns new data.

Actual behavior

  1. I get this warning:
Microsoft.Extensions.Caching.Hybrid.HybridCache[11]
      Cache backend data rejected: ParseFault.
      System.ArgumentException: Destination array was not long enough. Check the destination index, length, and the array's lower bounds. (Parameter 'destinationArray')
         at System.Array.CopyImpl(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
         at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length)
         at System.Array.CopyTo(Array array, Int32 index)
         at Microsoft.Extensions.Caching.Hybrid.Internal.HybridCachePayload.TryParse(ArraySegment`1 source, String key, TagSet knownTags, DefaultHybridCache cache, ArraySegment`1& payload, TimeSpan& remainingTime, PayloadFlags& flags, UInt16& entropy, TagSet& pendingTags, Exception& fault)
  1. /no-tags endpoint return data.

Regression?

This issue was present in the previous .net9.0 release.

Known Workarounds

No response

Configuration

.NET SDK:
 Version:           10.0.100
 Commit:            b0f34d51fc
 Workload version:  10.0.100-manifests.4c0ca8ba
 MSBuild version:   18.0.2+b0f34d51f

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.26200
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\10.0.100\

Packages:

Microsoft.Extensions.Caching.Hybrid                        10.0.0
Microsoft.Extensions.Caching.StackExchangeRedis            10.0.0

Other information

No response

Metadata

Metadata

Assignees

Labels

area-caching-hybridbugThis issue describes a behavior which is not expected - a bug.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions