Skip to content

Allocate the named mutex ownership chain before acquiring the lock - #137227

Merged
jkoritzinsky merged 2 commits into
mainfrom
dev/jkoritzinsky/named-mutex-chain-alloc-before-acquire
Nov 23, 2026
Merged

jkoritzinsky merged 2 commits into
mainfrom
dev/jkoritzinsky/named-mutex-chain-alloc-before-acquire

Conversation

@jkoritzinsky

Copy link
Copy Markdown
Member

The per-thread NamedMutexOwnershipChain is allocated lazily in ThreadWaitInfo, and the allocation happened in RecordMutexAcquisition, after the native lock had already been acquired and the owner, recursion count and process-data reference had been recorded. If that allocation threw (for example OutOfMemoryException), nothing was rolled back:

  • the process-data reference leaked, so the name was not retired until the process exited;
  • the failing thread still owned the lock, so a later ReleaseMutex (or a retried WaitOne followed by releases) looped forever in NamedMutexOwnershipChain.Remove while holding the process-wide creation/deletion lock, which blocked every other named-mutex operation in that process;
  • on the flock backend, where the chain is the only thing that releases the file lock when a thread dies, the lock stayed held for the lifetime of the process, blocking other processes.

The chain is only allocated on a thread's first named-mutex acquisition (32 bytes), so this only matters for that first acquisition.

Change

  • Add ThreadWaitInfo.EnsureNamedMutexOwnershipChain(), which allocates without requiring the creation/deletion process lock. The NamedMutexOwnershipChain getter keeps its lock assertion and calls it.
  • Call it before the underlying lock is acquired in WaitSubsystem.NamedMutex.Wait_Locked (after lockHolder.Dispose()) and in NamedMutexProcessDataBase.CreateOrOpen when acquireLockIfCreated is set. An allocation failure now happens before any native, shared or managed state changes.

Validation

Locally on Linux x64 (checked CoreLib):

  • Real OOM, using a child process with DOTNET_GCHeapHardLimit, and an injected OOM at the chain allocation, on both the pthread backend and (forced) the flock backend, for WaitOne and for new Mutex(initiallyOwned: true, ...). Before the change the mutex stayed held, the reference leaked, and a later release hung (pthread) or hit Debug.Assert(IsLockOwnedByCurrentThread) (flock). After the change the exception propagates, the mutex is not held, other threads and processes acquire it immediately, and a later ReleaseMutex throws InvalidOperationException.
  • System.Threading.Tests MutexTests: 119 total, 0 failed, 1 skipped (wasm-only condition).

Not covered: no regression test is included, because reproducing the failure needs an allocation-failure mechanism. Real OOM was only exercised on the pthread backend; the flock and CreateOrOpen rows used an injected failure and a forced flock backend on Linux x64. Other platforms were not run.

Note

This PR description was AI/Copilot-generated.

jkoritzinsky and others added 2 commits November 9, 2026 15:32
The per-thread NamedMutexOwnershipChain was allocated lazily in
RecordMutexAcquisition, after the native lock had been acquired and the
owner, recursion count and process-data reference had been recorded. If
that allocation threw (for example OutOfMemoryException), the lock stayed
held without a chain entry: the process-data reference leaked, a later
ReleaseMutex on that thread looped forever in
NamedMutexOwnershipChain.Remove, and on the flock backend the file lock
was held until the process exited.

Add ThreadWaitInfo.EnsureNamedMutexOwnershipChain, which allocates
without requiring the creation/deletion process lock, and call it before
the underlying lock is acquired in WaitSubsystem.NamedMutex.Wait_Locked
and in NamedMutexProcessDataBase.CreateOrOpen when the mutex is
initially owned.

Co-authored-by: Copilot <[email protected]>
Split the lazy allocation of the per-thread NamedMutexOwnershipChain out
of the NamedMutexOwnershipChain getter into a method that does not
require the creation/deletion process lock, so named mutex acquisition
can allocate the chain before the underlying lock is acquired.

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

@janvorli janvorli left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@jkoritzinsky

Copy link
Copy Markdown
Member Author

/ba-g http3 test failure

@jkoritzinsky
jkoritzinsky merged commit d5c8933 into main Nov 23, 2026
134 of 136 checks passed
@jkoritzinsky
jkoritzinsky deleted the dev/jkoritzinsky/named-mutex-chain-alloc-before-acquire branch November 23, 2026 21:42
@github-project-automation github-project-automation Bot moved this to Done in AppModel Nov 23, 2026
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 24, 2026
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.

2 participants