Skip to content

Commit

Permalink
Notify the debugger of a cross-thread dependency in Lock (dotnet#101501)
Browse files Browse the repository at this point in the history
* Notify the debugger of a cross-thread dependency in Lock

When a FuncEval tries to acquire a Lock that is held by a different thread, notify the debugger of a cross-thread dependency.
  • Loading branch information
kouvel authored and Ruihan-Yin committed May 30, 2024
1 parent 3b12766 commit fa52f3a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Threading/Lock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@ private ThreadId TryEnterSlow(int timeoutMs, ThreadId currentThreadId)
return new ThreadId(0);
}

//
// At this point, a full lock attempt has been made, and it's time to retry or wait for the lock.
//

// Notify the debugger that this thread is about to wait for a lock that is likely held by another thread. The
// debugger may choose to enable other threads to run to help resolve the dependency, or it may choose to abort the
// FuncEval here. The lock state is consistent here for an abort, whereas letting a FuncEval continue to run could
// lead to the FuncEval timing out and potentially aborting at an arbitrary place where the lock state may not be
// consistent.
Debugger.NotifyOfCrossThreadDependency();

if (LazyInitializeOrEnter() == TryLockResult.Locked)
{
goto Locked;
Expand Down

0 comments on commit fa52f3a

Please sign in to comment.