ACorns.Debugging - The .Net Deadlock Detector
July 30, 2008 .Net, Debugging, Improve Your Code, Tools, Visual Studio 10 CommentsAs nothing exciting has happed in my yard since the last release of Hawkeye I’ve decided to spice up my life and write a new .Net tool: The .Net Deadlock Detector.
The punch line: The .Net Deadlock Detector is the only* tool that is able to detect and report a deadlock inside a running .Net process in a production environment or out of a memory dump.
(* Disclaimer: I couldn’t find any other tool to do this. If you know of one please let me know)
Production environment is an environment in which you don’t (want to) have installed a debugging tool like Visual Studio.
The .Net Deadlock Detector
- The tool does not require to have the code re-compiled in any way or form, with any external dependencies, nor reference any external library or have you modify your code to use any special type of locks inside your code
- It works on release builds with no PDB files
- It works on running processes or previously captured memory dumps
- It detects deadlocks across multiple threads and returns detailed call-stack and lock usage information
- It only detect deadlocks in which threads are actively waiting for locks acquired by other threads
- It does not detect the dining philosophers problem or deadlocks created in combination of time waits + wake/check + lock
- It has an external dependency on the cdb.exe (part of the the free Debugging Tools for Windows package from Microsoft)
- It requires absolutely no installation. It an xcopy deployment
- And best of all it’s free (source code to be published soon)
What’s a deadlock
A deadlock is a situation wherein two or more competing actions are waiting for the other to finish, and thus neither ever does. It is often seen in a paradox like ‘the chicken or the egg‘. (wikipedia)
For example Thread 1 locks resource A, Thread 2 locks resource B, Thread 1 wants resource B and starts a wait on resource B, Thread 2 wants resource A and starts a wait on resource A.
In this moment the two threads are considered deadlocked as each of them owns a resource while trying to acquire another resource owned by a different thread.
- Two threads, two resources

- Thread 1 acquires resource A
- Thread 2 acquires resource B
- Thread 1 wants resource B and starts waiting for it
- Thread 1 wants resource A and starts waiting for it
Now both threads wait for the other one to release their resource.
How does the .Net Deadlock Detector work
The .Net Deadlock detector works by loading the cdb.exe (one of the native Windows Debuggers) on the target process and hooking the input and output streams of it to allow it to send commands and receive output from the debugger.
Then the tool is loading the sos (Son-on-Strike) debugging extensions into the cdb and starts sending commands to the cdb and sos and parse the output.
Then the tool follows a standard procedure in trying to find a managed deadlock by analysing the locks, the threads and the callbacks for each of the threads. As always one of the best examples to understand deadlocks is is Tess’s Deadlock case study.
So more or less the tool is a glorified macro system and command automation that is using standard cdb and sos commands to understand what is happening with the process and does some intensive analysis (including circular references search) to detect the deadlock.
How to “install” the .Net Deadlock Detector
- First of all before you can use the .Net Deadlock Detector you need to install (on your development machine not on the production machine) the Debugging Tools for Windows from Microsoft
- Then Download ACorns.Debugging.FindDeadlock.1.0.1.zip and unzip
- Now you need to copy the cdb.exe from the installation folder (defaults in C:\Program Files\Debugging Tools for Windows\cdb.exe) into the folder where the ACorns.Debugging.FindDeadlock.exe was unzipped
- You are now ready to use the tool
- You can now copy this new folder containing (ACorns.Debugging.Cdb.dll, ACorns.Debugging.FindDeadlock.exe and cdb.exe) to your production machine or target machine and start finding your deadlock
(Note: I’d love to deliver the a complete tool but the cdb.exe is not redistributable.)
How to use the .Net Deadlock Detector
If you finally got the right files prepared you are ready to try to find your deadlock.
The tool can be used with a set of command line parameters (exclusive):
ACorns.Debugging.FindDeadlock.exe [/pn=<processname>|/pid=<processid>|/DumpFile=<path to memory dump file>]
- /pn=name of a process
- /pid=id of a proces
- /DumpFile=path to the file
For example to try the tool on the provided demo application start the application then start the deadlock detector with:
ACorns.Debugging.FindDeadlock.exe /pn=ACorns.Debugging.DeadlockTests.exe
Interpreting the results
The tool will output a bunch of less relevant details as it tries to understand the deadlock and at the end it presents a “graphical” representation of the deadlock with some general details about the threads and the locks:
Thread 4 owns a lock named B and waits for lock A. Thread 3 owns the lock A and waits for B.
Then the most relevant part of the analysis comes: the callstack involved in the deadlock. (the callstacks should be read bottom to top with the Using of locks referring to the next (up) method)
Thread 4 has the following callstack:
We can see that method StartThraed2 is using lock B and the Thread2Worker is trying to use lock A.
Thread 3 has the following callstack:
Method StartThread1 is using lock A and Thread1Worker is trying to use lock B.
With all this information at hand you should be able to find and fix your deadlock. Good luck!
If you have a memory dump of a process that has a deadlock that the tool can’t detect please let me know as I’d like to debug it and improve the tool.
Now, start the download of the ACorns.Debugging.FindDeadlock.1.0.1.zip tool and then head to Microsoft to download the Debugging Tools for Windows.
The tool is based on an idea by Tatham Oddie and Paul Stovell. Thanks guys!