Skip to content

[release/11.0] Make Unix named mutex timed waiter count atomic and exception safe - #138161

Merged
JulieLeeMSFT merged 1 commit into
release/11.0from
backport/pr-137221-to-release/11.0
Nov 24, 2026
Merged

JulieLeeMSFT merged 1 commit into
release/11.0from
backport/pr-137221-to-release/11.0

Conversation

@github-actions

@github-actions github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #137221 to release/11.0

/cc @jkoritzinsky

Customer Impact

  • Customer reported
  • 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

  • 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.

…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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.

@JulieLeeMSFT

Copy link
Copy Markdown
Member

/ba-g known issues.

@JulieLeeMSFT
JulieLeeMSFT merged commit 58f10f1 into release/11.0 Nov 24, 2026
134 of 138 checks passed
@JulieLeeMSFT
JulieLeeMSFT deleted the backport/pr-137221-to-release/11.0 branch November 24, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Threading Servicing-consider Issue for next servicing release review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants