Fix CA2016: pass cancellation token to semaphore.Wait in MemoryCacheBase - #818
Fix CA2016: pass cancellation token to semaphore.Wait in MemoryCacheBase#818KrzysztofPajak with Copilot wants to merge 5 commits into
Conversation
Co-authored-by: KrzysztofPajak <16772986+KrzysztofPajak@users.noreply.github.com>
There was a problem hiding this comment.
🟡 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.Tokentosemaphore.Wait(...)inMemoryCacheBase.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.
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>
|



semaphore.Wait()inMemoryCacheBase.Get<T>was called without aCancellationToken, triggering a CA2016 analyzer warning and leaving the blocking wait unable to be cancelled via the class's existing reset token.Changes
_resetCacheToken.Tokentosemaphore.Wait()inMemoryCacheBase.Get<T>, consistent with how the same token is already used elsewhere in the class (e.g.AddExpirationToken).