Skip to content

Add retry policy to NuCache content store lock acquisition#19429

Closed
AndyButland wants to merge 3 commits intov13/devfrom
v13/bugfix/add-retry-to-nucache-content-store-lock-acquisition
Closed

Add retry policy to NuCache content store lock acquisition#19429
AndyButland wants to merge 3 commits intov13/devfrom
v13/bugfix/add-retry-to-nucache-content-store-lock-acquisition

Conversation

@AndyButland
Copy link
Copy Markdown
Contributor

@AndyButland AndyButland commented May 27, 2025

Prerequisites

  • I have added steps to test this contribution in the description below

Resolves: #19338

Description

The linked issue reports a regression after #17246, which was introduced to avoid locking exceptions in asynchronous code.

It seems that after this, near-simultaneous requests to save and publish content results in one or more of the requests failing due to NuCache being unable to acquire a lock (the SemaphoneSlim that's it's based on not being released from the previous operation). You would then find the content created and published, but the failed ones wouldn't be in the cache, which is manifested in the backoffice by the message of "published but not in the cache" shown where the URLs are displayed on the "Info" panel.

To resolve I've added a retry strategy that will retry on an incrementally delayed basis for up to 2 second if the lock can't be acquired.

It's appreciated that this may not be the most elegant approach nor proposed with the deepest understanding of how the NuCache is working, but given this is a regression in a component that's now legacy, I think it'll be a reasonable fix for the issue.

Testing

The original issue can be replicated as shown in the linked issue from the browser console when logged into the backoffice.

You should find with this PR in place multiple requests to create, save and publish content will succeed.

Copilot AI review requested due to automatic review settings May 27, 2025 10:24
@AndyButland AndyButland changed the title Add locking to NuCache content store lock acquisition Add retry policy to NuCache content store lock acquisition May 27, 2025
Copy link
Copy Markdown
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.

Pull Request Overview

This PR addresses a regression in the NuCache content store locking mechanism by adding a retry strategy with incremental backoff.

  • Introduces a call to WaitForWriteLock before attempting to acquire the lock
  • Implements WaitForWriteLock which retries waiting for the lock with incremental delay
  • Updates inline comments to document the new retry strategy

_logger.LogDebug("Waiting for write lock, attempt {Attempt} of {Retries}. Sleeping for {DelayFor} milliseconds.", retryCount, retries, delayFor);
}

Thread.Sleep(delayFor);
Copy link

Copilot AI May 27, 2025

Choose a reason for hiding this comment

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

Consider using an asynchronous wait mechanism instead of Thread.Sleep to avoid blocking threads in potentially async contexts.

Suggested change
Thread.Sleep(delayFor);
await Task.Delay(delayFor);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Don't believe we can do this here, or at least we wouldn't get any benefit, as this is called from synchronous code.

@AndyButland AndyButland force-pushed the v13/bugfix/add-retry-to-nucache-content-store-lock-acquisition branch from 6f89b70 to f0c83ae Compare May 27, 2025 10:25
… the check before the wait that threw the recursive lock exception.

Added additional check to ensure we don't release a lock that's already released.
@AndyButland AndyButland deleted the v13/bugfix/add-retry-to-nucache-content-store-lock-acquisition branch May 27, 2025 13:18
@AndyButland
Copy link
Copy Markdown
Contributor Author

Closed in favour of #19434

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants