Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting deadlocks in a C# application [duplicate]

Tags:

Possible Duplicate:
C#/.NET analysis tool to find race conditions/deadlocks

I am debugging an application that I suspect is getting deadlocked and hanging. However, this only occurs every few days, and it never happens on my computer so I can't hook a debugger up to it. Are there any utilities or methods I can use to query the running application and find out what methods/locks/whatever it is deadlocked on?

Update: Typically the application is running at a customer location and I don't have access to the machine, and I'm not entirely comfortable asking them to install tons of software.

like image 679
Jon Tackabury Avatar asked Feb 03 '09 18:02

Jon Tackabury


People also ask

How deadlocks can be detected?

If resources have a single instance – In this case for Deadlock detection, we can run an algorithm to check for the cycle in the Resource Allocation Graph. The presence of a cycle in the graph is a sufficient condition for deadlock.

How do you detect a deadlock and fix it?

There is one more method to detect Deadlock in Java, it can be done by running the program in CMD. All we need to do is collect thread dumps and then we have to command to collect, depending upon the operating system. If we are running Java 8 on windows, a command would be jcmd $PID Thread.

How deadlocks can be detected and prevented?

Deadlock can be prevented by eliminating any of the four necessary conditions, which are mutual exclusion, hold and wait, no preemption, and circular wait. Mutual exclusion, hold and wait and no preemption cannot be violated practically.

Which algorithm is used for detecting deadlocks?

The Banker's algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra. This prevents a single thread from entering the same lock more than once.


1 Answers

You can use WinDbg to inspect the threads in the application. Here's a brief plan of what you could do.

  • When the application hangs, copy the WinDbg files to the machine.
  • Either attach WinDbg to the process or use ADPlus to get a hang dump of the process. If you choose ADPlus, you then load the dump in WinDbg.
  • From WinDbg you load sos.dll, so you can inspect managed code.
  • The !threads command will show you all threads in the application and the !clrstack command, will show you what they are doing. Use ~e!clrstack to dump the call stack of all threads. Look for calls to Wait methods as they indicate locking.
  • The !syncblk command will give you information of what threads are holding the different locks.
  • To find out what lock a given thread is trying to acquire, switch to the thread and inspect stack objects (!dso). From here you should be able to find the lock the thread is trying to acquire.

Clarification: WinDbg doesn't require a regular install. Just copy the files. Also, if you take the hang dump, you can continue debugging on another machine if so desired.

Addition: Sosex has the !dlk command that automatically identifies deadlocks in many situations. It doesn't work all the time, but when it does, it does all the work for you, so that should be your first choice.

like image 98
Brian Rasmussen Avatar answered Sep 21 '22 19:09

Brian Rasmussen