How to: Debug Deadlocks Using Windbg?

  • Attach the debugger to the process
  • Load SOS
  • ~* e !clrstack to view the CLR stack of all the threads and where they are
  • Look for System.Threading.Monitor.Enter(System.Object) in the top frames
  • Load SOSEX.dll from https://www.stevestechspot.com/SOSEXUpdatedV11Available.aspx
  • !dlk to detect deadlocks. The output looks like the following:

Deadlock detected:

CLR thread 5 holds sync block 0018f05c OBJ:010d909c[Microsoft.Sample.SampleEvent]

             waits sync block 0018f02c OBJ:010d7704[System.Object]

CLR thread 6 holds sync block 0018f02c OBJ:010d7704[System.Object]

             waits sync block 0018f05c OBJ:010d909c[Microsoft.Sample.SampleEvent]

CLR Thread 5 is waiting at Microsoft.Sample.Connection.Register(Microsoft.Sample.EventInfo)(+0x0 IL)(+0xd Native) [e:\bt\815863\Connection.cs, @ 11,9]

CLR Thread 6 is waiting at Microsoft.Sample.Trigger.Initialize()(+0x0 IL)(+0xa Native) [e:\bt\815863\Trigger.cs, @ 31,9]

1 deadlock detected.

  • ~<ThreadId> e !clrstack to view the CLR stack of that thread
  • Look at the code and fix the issue