PROD-5987: fix race condition in HzCacheMemoryLocker causing disposed semaphore errors#26
Merged
JoelNygren-Norce merged 2 commits intoJun 10, 2026
Conversation
…evicted Catch ObjectDisposedException in both AcquireLockAsync and AcquireLock. When MemoryCache evicts and disposes a SemaphoreSlim between GetSemaphore and WaitAsync, remove the stale entry and retry with a fresh semaphore instead of propagating the exception as a fatal error. PROD-5987 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Replace single retry with a for-loop bounded by maxLockRetries (default 3). The catch filter (when attempt < maxLockRetries) lets the final attempt's ObjectDisposedException propagate if retries are exhausted. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses a race in HzCacheMemoryLocker where SemaphoreSlim instances stored in MemoryCache can be evicted/disposed between GetSemaphore() and Wait()/WaitAsync(), causing ObjectDisposedException to escape and be treated as a fatal “Could not acquire lock” error.
Changes:
- Added
maxLockRetriestoHzCacheMemoryLockerOptionsto control retry behavior on disposed semaphores. - Wrapped
SemaphoreSlim.WaitAsync()in a retry loop that removes the stale cache entry and re-creates the semaphore whenObjectDisposedExceptionoccurs. - Applied the same retry pattern to the synchronous
SemaphoreSlim.Wait()path.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
patrik-j-nilsson
approved these changes
Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary\n\n
HzCacheMemoryLockerstoresSemaphoreSliminstances in aMemoryCachewith 5-minute sliding expiration. The post-eviction callback disposes the semaphore, but there's a race betweenGetSemaphore()returning a reference andWaitAsync()/Wait()using it — theMemoryCachecan evict+dispose the semaphore in that window, causingObjectDisposedExceptionto propagate as a fatal "Could not acquire lock" error.\n\nFix: catchObjectDisposedExceptionin bothAcquireLockAsyncandAcquireLock, remove the stalelockCacheentry, and retry with a fresh semaphore:\n\ndiff\n+try\n+{\n acquired = await semaphore.WaitAsync(timeout, token);\n+}\n+catch (ObjectDisposedException)\n+{\n+ lockCache.Remove(key);\n+ semaphore = GetSemaphore(cacheName, cacheInstanceId, key, logger);\n+ acquired = await semaphore.WaitAsync(timeout, token);\n+}\n\n\nObserved in production Admin on 2026-06-10 (09:07–09:52 UTC, ~2,375 errors). The race was amplified by background serialization failures (StartUpdateChecksumAndNotify) causing contention and GC pressure.\n\nJira: PROD-5987">Link to Devin session: https://app.devin.ai/sessions/3e7c5915fd8640e69f53c7ad184e302d
Requested by: @JoelNygren-Norce