We have an C# console application in production which has intermittent thread deadlock. I tried to find deadlock by attaching VS 2017 to the running process, but I could not find any way to easily find the cause of the deadlock. There didn't seem to be any window which identifies which threads are owning which locks. (I tried using the Memory window, but that didn't work at all).
I have also tried using Dump file but found it very hard to make sense of what it shows. (But this was before I knew I was searching for a deadlock.)
I am used to using JStack in Java, a command line utility to run against running Java application, which prints a Thread dump, and identifies deadlocks, and shows in each StackTrace the points where a Thread has locked a monitor.
Is there some equivalent tool for .NET?
I'd use WinDbg to examine a full userdump taken from the process when the deadlock happens. Make sure you have the exact binaries (DLL-s and PDB-s) along with the dump file. Use the appropriate WinDbg version for your binaries (32-bit or 64-bit).
Open your dump using the File
->Open crash dump...
command, this will open a "console" window in WinDbg. You use it by typing commands in the bottom input area. You can save all WinDbg output by logging it in the Edit
menu.
You can load the SOS extension using .loadby sos clr
and then get all the call stacks using !EEStack
. You can try using the -short
parameter to see if you spot the same function on top of the threads.
As @dmitry-egorov suggested in the comments, you can also use the !dlk
from SOSEX.
When you're looking for functions that may cause the deadlock, look for your functions - they may not be at the very top of the stack but will be close - some of your functions are likely trying to take 2 different locks in different orders and they deadlock.