Skip to content

Fix CA2016: pass cancellation token to semaphore.Wait in MemoryCacheBase - #818

Closed
KrzysztofPajak with Copilot wants to merge 5 commits into
developfrom
copilot/pass-cancellation-token-to-method
Closed

Fix CA2016: pass cancellation token to semaphore.Wait in MemoryCacheBase#818
KrzysztofPajak with Copilot wants to merge 5 commits into
developfrom
copilot/pass-cancellation-token-to-method

Conversation

Copilot AI commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

semaphore.Wait() in MemoryCacheBase.Get<T> was called without a CancellationToken, triggering a CA2016 analyzer warning and leaving the blocking wait unable to be cancelled via the class's existing reset token.

Changes

  • Pass _resetCacheToken.Token to semaphore.Wait() in MemoryCacheBase.Get<T>, consistent with how the same token is already used elsewhere in the class (e.g. AddExpirationToken).
var semaphore = CacheEntries.GetOrAdd(key, _ => new SemaphoreSlim(1, 1));
semaphore.Wait(_resetCacheToken.Token);

Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

semaphore.Wait(_resetCacheToken.Token) can throw OperationCanceledException during Clear() and currently bubbles out of Get<T> without handling, risking runtime failures.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR updates MemoryCacheBase’s synchronous Get<T> cache-miss path to pass the class reset CancellationToken into SemaphoreSlim.Wait(...), addressing CA2016 and aligning the semaphore wait with the existing cache-reset token mechanism used for entry expiration.

Changes:

  • Pass _resetCacheToken.Token to semaphore.Wait(...) in MemoryCacheBase.Get<T>(...).

Validation: Not run (code review only).

File summaries
File Description
src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs Passes the cache reset token into the synchronous semaphore wait to satisfy CA2016 and support cancellation during cache reset.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/Core/Grand.Infrastructure/Caching/MemoryCacheBase.cs
Copilot AI and others added 2 commits September 6, 2026 11:04
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Sep 6, 2026

Copy link
Copy Markdown

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.

3 participants