Repository navigation
Allocate the named mutex ownership chain before acquiring the lock - #137227
Merged
jkoritzinsky merged 2 commits intoNov 23, 2026
Merged
Conversation
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: 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 |
Member
Author
|
/ba-g http3 test failure |
jkoritzinsky
deleted the
dev/jkoritzinsky/named-mutex-chain-alloc-before-acquire
branch
November 23, 2026 21:42
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 per-thread
NamedMutexOwnershipChainis allocated lazily inThreadWaitInfo, and the allocation happened inRecordMutexAcquisition, 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 exampleOutOfMemoryException), nothing was rolled back:ReleaseMutex(or a retriedWaitOnefollowed by releases) looped forever inNamedMutexOwnershipChain.Removewhile holding the process-wide creation/deletion lock, which blocked every other named-mutex operation in that process;The chain is only allocated on a thread's first named-mutex acquisition (32 bytes), so this only matters for that first acquisition.
Change
ThreadWaitInfo.EnsureNamedMutexOwnershipChain(), which allocates without requiring the creation/deletion process lock. TheNamedMutexOwnershipChaingetter keeps its lock assertion and calls it.WaitSubsystem.NamedMutex.Wait_Locked(afterlockHolder.Dispose()) and inNamedMutexProcessDataBase.CreateOrOpenwhenacquireLockIfCreatedis set. An allocation failure now happens before any native, shared or managed state changes.Validation
Locally on Linux x64 (checked CoreLib):
DOTNET_GCHeapHardLimit, and an injected OOM at the chain allocation, on both the pthread backend and (forced) the flock backend, forWaitOneand fornew Mutex(initiallyOwned: true, ...). Before the change the mutex stayed held, the reference leaked, and a later release hung (pthread) or hitDebug.Assert(IsLockOwnedByCurrentThread)(flock). After the change the exception propagates, the mutex is not held, other threads and processes acquire it immediately, and a laterReleaseMutexthrowsInvalidOperationException.System.Threading.TestsMutexTests: 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
CreateOrOpenrows 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.