Skip to content

Cache: Add awaits to memory cache rebuilds to fix race conditions#20960

Merged
AndyButland merged 2 commits intomainfrom
v17/bugfix/fix-race-condition-in-cache-refreshers
Nov 26, 2025
Merged

Cache: Add awaits to memory cache rebuilds to fix race conditions#20960
AndyButland merged 2 commits intomainfrom
v17/bugfix/fix-race-condition-in-cache-refreshers

Conversation

@nikolajlauridsen
Copy link
Contributor

@nikolajlauridsen nikolajlauridsen commented Nov 26, 2025

What does this PR fix?

This PR adds missing await keywords on async cache operations that were introduced in #17296.

Context

With the introduction of hybrid cache rebuilding, memory cache was added to cache refreshers in #17296. During this process, awaits were missed on async methods (see this example), meaning the run in parallel, which can't work.

This has caused race conditions which can result in various issues such as multiple open DataReaders and failure to acquire locks.

This issue becomes particularly apparent when adding an L2 cache like Redis, likely because hybridCache.RemoveAsync becomes truly asynchronous in that scenario. Additionally, this issue only affects SQL Server.

Testing

Due to the nature of race conditions, this is difficult to test reliably. However, the issue can be reproduced consistently by:

  1. Installing using SQL Server
  2. Creating and configuring Umbraco to use a Redis cache
  3. Creating a document type
  4. Creating content using the document type
  5. Attempting to edit the document type
  6. Failure occurs reliably

Redis cache configuration example:

Install Microsoft.Extensions.Caching.StackExchangeRedis

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "{INSTANCE ID}";
    options.InstanceName = "{INSTANCE NAME}";
});

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Copy link
Contributor

@AndyButland AndyButland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I can review and approve this with just visual inspection, as it's clearly an existing oversight and a correct update.

@AndyButland AndyButland merged commit eaf5960 into main Nov 26, 2025
26 checks passed
@AndyButland AndyButland deleted the v17/bugfix/fix-race-condition-in-cache-refreshers branch November 26, 2025 14:02
nikolajlauridsen added a commit that referenced this pull request Dec 1, 2025
…0960)

* Await rebuilds and fix multiple open DataReaders

* Add additional missing awaits

(cherry picked from commit eaf5960)
@nikolajlauridsen
Copy link
Contributor Author

Cherry picked for 17.0.1 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants