Repository navigation
[release/11.0] Make Unix named mutex timed waiter count atomic and exception safe - #138161
Merged
Merged
Conversation
…137221) 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. `AcquireLockCore` updated it with plain `++`/`--`, which are not atomic across processes (RyuJIT emits `inc`/`dec dword ptr [mem]` without `LOCK` on x64; arm64 uses a separate load/add/store), and it did not restore the count when a timed wait left through an exception such as `ThreadInterruptedException` from the polling `Thread.Sleep`. Concurrent timed waiters in different processes could therefore lose increments/decrements and wrap the `uint` to `0xFFFFFFFF`, and an interrupted wait left the count permanently incremented. A wrongly non-zero count makes every later infinite wait on that name poll with `Thread.Sleep(100)` instead of blocking in `flock`; a wrongly zero count removes the guard that keeps untimed waiters from starving timed waiters. The pre-managed PAL implementation used `InterlockedIncrement`/`InterlockedDecrement` with an RAII decrement. This change: - updates the shared count with `Interlocked.Increment`/`Interlocked.Decrement` and uses `Volatile` for the other accesses, - decrements it in a `finally` so every exit path (timeout, acquisition, exception) restores it, - adds `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 `UsePThreadMutexes` was forced to `false` (not part of this PR), on the base the same files had at `8a1c05df905` plus these changes: - New test without the fix: 1 run, 1 failed (count is `1` instead of `0` after the interrupted wait). - New test with the fix: 1 run, 0 failed. - Full `System.Threading.Tests` `MutexTests` class with the fix: 120 run, 0 failed, 1 skipped. - Cross-process race repro from the issue (3 waiters x 3000 and 2 waiters x 6000 iterations, `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 `main` tip (it merges cleanly into it). Resolves #137219 > [!NOTE] > This pull request was prepared with the help of GitHub Copilot (an AI assistant).
|
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 |
This was referenced Nov 24, 2026
JulieLeeMSFT
approved these changes
Nov 24, 2026
Member
|
/ba-g known issues. |
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.
Backport of #137221 to release/11.0
/cc @jkoritzinsky
Customer Impact
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
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.