Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ private static bool GetIsMultithreadingSupported()
public static bool IsWasmThreadingSupported => IsWasm && IsMultithreadingSupported;
public static bool IsNotWasmThreadingSupported => !IsWasmThreadingSupported;

// Whether managed cleanup of externally created threads is deferred to the finalizer thread.
public static bool ManagedThreadCleanupOnFinalizerThread => IsCoreCLR || IsMonoRuntime;

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.

This is a very corner-case condition. It may be better if it lives with the one test that uses it.


private static readonly Lazy<bool> s_isBinaryFormatterSupported = new Lazy<bool>(DetermineBinaryFormatterSupport);
public static bool IsBinaryFormatterSupported => s_isBinaryFormatterSupported.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,18 @@ public void Abandon(NamedMutexOwnershipChain chain, Thread abandonedThread)
SharedMemoryManager<NamedMutexProcessDataBase>.Instance.VerifyCreationDeletionProcessLockIsLocked();

// This method can be called from one of two threads:
// 1. The dead thread that owns the mutex.
// In the first case, we know that no one else can acquire the lock, so we can safely
// set the lock count to 0 and abandon the mutex.
// 1. The exiting thread that owns the mutex.
// No one else can acquire the mutex yet, so we can mark it abandoned and release it.
// 2. The finalizer thread after the owning thread has died.
// In this case, its possible for the mutex to have been acquired by another thread
// after the owning thread died if it is backed by a pthread mutex.
// A pthread mutex doesn't need our Abandon() call to be abandoned, it will be automatically abandoned
// when the owning thread dies.
// In this case, we don't want to do anything. Our named mutex implementation will handle the abandonment
// as part of acquisition.
Debug.Assert(IsLockOwnedByCurrentThread || (!abandonedThread.IsAlive && Thread.CurrentThreadIsFinalizerThread()),
// For pthread-backed mutexes, native thread exit automatically abandons the mutex, and another thread
// or process may already have acquired it. In this case, only retire the dead thread's local ownership;
// deferred cleanup must not modify shared state or release the native mutex.
// In both cases, use local ownership to identify the bookkeeping to retire, since another process can change
// the shared owner IDs without taking our creation/deletion process lock.
Debug.Assert(Thread.CurrentThread == abandonedThread || (!abandonedThread.IsAlive && Thread.CurrentThreadIsFinalizerThread()),
"Abandon can only be called from the thread that owns the lock or from the finalizer thread when the owning thread is dead.");

if (!IsLockOwnedByThreadInThisProcess(abandonedThread))
if (_lockOwnerThread != abandonedThread)
{
Debug.Assert(Thread.CurrentThreadIsFinalizerThread());
// Lock is owned by a different thread already, so we don't need to do anything.
Expand All @@ -175,20 +173,24 @@ public void Abandon(NamedMutexOwnershipChain chain, Thread abandonedThread)
return;
}

IsAbandoned = true;

_lockCount = 0;

Debug.Assert(_lockOwnerThread is not null);

ReleaseLockCore(abandoning: true);
AbandonLockCore(abandonedThread);

chain.Remove(this);
_lockOwnerThread = null;
// Remove the refcount from the thread's wait info.
_processDataHeader.DecrementRefCount();
}

protected virtual void AbandonLockCore(Thread abandonedThread)
{
IsAbandoned = true;
ReleaseLockCore(abandoning: true);
}

protected abstract MutexTryAcquireLockResult AcquireLockCore(int timeoutMilliseconds);

protected abstract bool IsAbandoned { get; set; }
Expand Down Expand Up @@ -303,6 +305,14 @@ public MutexTryAcquireLockResult RecordMutexAcquisition(WaitSubsystem.ThreadWait

SharedMemoryManager<NamedMutexProcessDataBase>.Instance.VerifyCreationDeletionProcessLockIsLocked();

// A robust mutex can be recovered before the dead owner's managed cleanup runs.
Comment thread
jkoritzinsky marked this conversation as resolved.
Resolved
if (mutexData._lockOwnerThread is Thread previousOwner)
{
Debug.Assert(!previousOwner.IsAlive);
previousOwner.WaitInfo.NamedMutexOwnershipChain.Remove(mutexData);
mutexData._processDataHeader.DecrementRefCount();
}

mutexData.SetLockOwnerToCurrentThread();
mutexData._lockCount = 1;
mutexData._lockOwnerThread = waitInfo.Thread;
Expand Down Expand Up @@ -375,9 +385,8 @@ protected override MutexTryAcquireLockResult AcquireLockCore(int timeoutMillisec
return result;
}

if (_lockCount != 0)
if (_lockCount != 0 && IsLockOwnedByCurrentThread)
{
Debug.Assert(IsLockOwnedByCurrentThread);
Debug.Assert(!IsAbandoned);
try
{
Expand All @@ -397,6 +406,16 @@ protected override MutexTryAcquireLockResult AcquireLockCore(int timeoutMillisec
return result;
}

protected override void AbandonLockCore(Thread abandonedThread)
{
// Native thread exit automatically abandons robust pthread mutexes. Deferred cleanup must not
// modify shared state or unlock a mutex that another process may already own.
if (Thread.CurrentThread == abandonedThread)
{
base.AbandonLockCore(abandonedThread);
}
}

protected override void ReleaseLockCore(bool abandoning)
{
Debug.Assert(_lockCount == 0);
Expand Down
Loading
Loading