Skip to content

Make Unix named mutex timed waiter count atomic and exception safe - #137221

Merged
jkoritzinsky merged 2 commits into
dotnet:mainfrom
jkoritzinsky:fix-named-mutex-timed-waiter-count
Nov 23, 2026
Merged

jkoritzinsky merged 2 commits into
dotnet:mainfrom
jkoritzinsky:fix-named-mutex-timed-waiter-count

Conversation

@jkoritzinsky

Copy link
Copy Markdown
Member

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

…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

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.

@jkoritzinsky

Copy link
Copy Markdown
Member Author

/ba-g Process test failure unrelated

@jkoritzinsky
jkoritzinsky merged commit d259552 into dotnet:main Nov 23, 2026
134 of 136 checks passed
@github-project-automation github-project-automation Bot moved this to Done in AppModel Nov 23, 2026
@jkoritzinsky

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 24, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Unix named mutex (file-lock backend): shared TimedWaiterCount is updated non-atomically and leaks on Thread.Interrupt

2 participants