Repository navigation
Make Unix named mutex timed waiter count atomic and exception safe - #137221
Merged
jkoritzinsky merged 2 commits intoNov 23, 2026
Merged
Conversation
…nThe file-lock named mutex backend keeps a timed waiter count in shared\nmemory that is read by every process using the mutex. It was updated\nwith plain increments and decrements, so concurrent timed waits in\ndifferent processes could lose updates and wrap the count, and a wait\nthat exited through an exception (for example Thread.Interrupt during the\npolling sleep) never decremented it.\n\nUse Interlocked operations for the shared count, volatile access for the\nreads, and decrement it in a finally block.\n\nCo-authored-by: Copilot <[email protected]>
…ex\n\nVerify that the shared timed waiter count is restored when a timed wait\non the Linux arm/arm64 file-lock named mutex backend is interrupted.\n\nCo-authored-by: Copilot <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov |
janvorli
approved these changes
Nov 19, 2026
Member
Author
|
/ba-g Process test failure unrelated |
Member
Author
|
/backport to release/11.0 |
Contributor
|
Started backporting to |
2 of 4 tasks
JulieLeeMSFT
pushed a commit
that referenced
this pull request
Nov 24, 2026
…ception safe (#138161) Backport of #137221 to release/11.0 /cc @jkoritzinsky ## Customer Impact - [ ] Customer reported - [X] Found internally Named mutexes on Linux arm32, Linux arm64, and macOS can deadlock or unexpectedly allow threads into the protected region incorrectly when threads race to acquire the mutex. ## Regression - [X] Yes - [ ] No Regressed in the ## Testing Local validation with a runtime that uses this named mutex backend on linux x64 was able to reproduce in 0.03%-0.05% of scenarios. Also an additional unit test that forces the race has been added in this PR. ## Risk Low, targeted fix in an isolated area. Co-authored-by: Jeremy Koritzinsky <[email protected]>
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.
The file-lock (non-pthread) named mutex backend keeps a timed waiter count in the shared-memory file that is visible to every process using the mutex.
AcquireLockCoreupdated it with plain++/--, which are not atomic across processes (RyuJIT emitsinc/dec dword ptr [mem]withoutLOCKon x64; arm64 uses a separate load/add/store), and it did not restore the count when a timed wait left through an exception such asThreadInterruptedExceptionfrom the pollingThread.Sleep.Concurrent timed waiters in different processes could therefore lose increments/decrements and wrap the
uintto0xFFFFFFFF, and an interrupted wait left the count permanently incremented. A wrongly non-zero count makes every later infinite wait on that name poll withThread.Sleep(100)instead of blocking inflock; a wrongly zero count removes the guard that keeps untimed waiters from starving timed waiters. The pre-managed PAL implementation usedInterlockedIncrement/InterlockedDecrementwith an RAII decrement.This change:
Interlocked.Increment/Interlocked.Decrementand usesVolatilefor the other accesses,finallyso every exit path (timeout, acquisition, exception) restores it,NamedMutexFileLockTests.InterruptedTimedWait_RestoresTimedWaiterCount, which reads the real shared count from the shm file across processes. It only runs on Linux arm/arm64 (where the file-lock backend is the default), so it is skipped on x64.Validation
Linux x64 uses the pthread backend, so I validated with a local build where only
UsePThreadMutexeswas forced tofalse(not part of this PR), on the base the same files had at8a1c05df905plus these changes:1instead of0after the interrupted wait).System.Threading.TestsMutexTestsclass with the fix: 120 run, 0 failed, 1 skipped.WaitOne(10)): no iterations with a non-zero count after all waits returned with the fix, versus 9 and 3 before the fix.I did not run on a stock Linux arm/arm64 or macOS build, and the branch itself was not built on the current
maintip (it merges cleanly into it).Resolves #137219
Note
This pull request was prepared with the help of GitHub Copilot (an AI assistant).