Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find locked objects

Tags:

c#

My application is in a deadlock. Is there any ways to identify the objects which are presently locked (as shown below)?


void DoWork()
{
   lock(this._lockObj)
   {
      // Do some work
   }
}
like image 938
Maanu Avatar asked Mar 12 '26 13:03

Maanu


2 Answers

The best way to do this is to use WinDbg and the SOS extension. It has a command named !SyncBlk that provides just this information. Here is a link to a quick tutorial

  • http://dotnetdebug.net/2006/02/23/syncblk-in-sos-for-net-framework-20/
like image 77
JaredPar Avatar answered Mar 14 '26 03:03

JaredPar


If you're debugging under Visual Studio, it's not too hard. You'll need two debug windows in particular: Call stack and Threads. Pause the program, and then in the thread window double click on each thread, to find where it's currently stopped. The deadlocking threads should have their execution stopped on a "lock" statement.

Then, on each of the deadlocked threads, you can trace your way up the stack to find the other lock. Just double click each method on the call and look at the context until you find another lock that you're inside of.

like image 42
Bryce Wagner Avatar answered Mar 14 '26 02:03

Bryce Wagner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!