编程语言
首页 > 编程语言> > 使用windbg在挂起的C#应用​​程序中检测死锁

使用windbg在挂起的C#应用​​程序中检测死锁

作者:互联网

我在C#2.0中使用线程池的多线程应用在产品中每隔几周挂起一次.

我做了一个同步

!syncblk

Index SyncBlock MonitorHeld Recursion Owning Thread Info  SyncBlock Owner
  201 05b9493c          979         1 05bc1040   bcc  45   022f3490 System.Collections.ArrayList
 2875 05b4c914            1         1 17b99e10  1af8 290   024862d8 MyProg.MyChildClass
 3045 05b4dbec            1         1 17ca7e98  1990 664   02392120 MyProg.MyChildClass
 3046 05b4dc1c            1         1 17ca8668  1038 666   02394b00 MyProg.MyChildClass
 3194 05b4f80c            3         1 05b5b638  1594  31   02537b88 MyProg.MyChildClass 
 3072 05b4e114            1         1 17d660f0  1120 848   023dd578 MyProg.MyChildClass
 3073 05b4e144            1         1 17c45c30  1884 518   023dfc2c MyProg.MyChildClass 
 3390 05b51cfc            3         1 16cefcc0  1350 102   02768868 MyProg.MyChildClass
 3072 05b4e114            1         1 17d660f0  1120 848   023dd578 MyProg.MyChildClass
 3073 05b4e144            1         1 17c45c30  1884 518   023dfc2c MyProg.MyChildClass 

我删除了几百行如下所示的行:

3073 05b4e144            1         1 17c45c30  1884 518   023dfc2c MyProg.MyChildClass 

拥有979个监视器的线程正在锁定一个数组列表,但是所有其他正在等待的线程都试图在另一个对象上获取读者锁.

我确实知道所有读取器线程都试图获取哪个锁,但是我不知道谁持有该锁.我怎么发现呢?

MonitorHeld = 1的所有线程具有类似的clrstack和dso输出.
这是持有979个锁(id = 45)和等待读取器线程(id = 290)的dso和clrstack输出.同样,当我做
〜* e!clrstack
Monitor.Enter的所有线程在以下2个线程上具有与clrstack相同的堆栈

0:000> ~45e !clrstack
OS Thread Id: 0xbcc (45)
ESP       EIP     
17fbe590 77d2013d [GCFrame: 17fbe590] 
17fbe6cc 77d2013d [HelperMethodFrame: 17fbe6cc] System.Threading.Monitor.Enter(System.Object)
17fbe720 00638427 MyProg.MyParentClass.ClockTimeElapsed(System.DateTime)
17fbe798 00637cf6 MyProg.MyEngine.ClockTimeElapsed(System.Object)
17fbe830 71df843f System.Threading._TimerCallback.TimerCallback_Context(System.Object)
17fbe838 71e302ff System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
17fbe850 71df83ab System.Threading._TimerCallback.PerformTimerCallback(System.Object)
17fbe9dc 74071b4c [GCFrame: 17fbe9dc] 
0:000> ~45e!dso
OS Thread Id: 0xbcc (45)
ESP/REG  Object   Name
17fbe5a8 022a2ff4 System.Globalization.DaylightTime
17fbe5ac 022a2f3c System.Int32
17fbe5c8 02537b88 MyProg.MyChildClass
17fbe738 02537b88 MyProg.MyChildClass
17fbe740 02537b88 MyProg.MyChildClass
17fbe7ac 11572f28 System.Collections.ArrayList+ArrayListEnumeratorSimple
17fbe7b0 022f3490 System.Collections.ArrayList
17fbe7b4 0231860c System.String    Error loading MyConfig.txt
17fbe7c0 022f3014 MyProg.MyEngine
17fbe84c 0231b48c System.Threading._TimerCallback
17fbe9fc 0231b48c System.Threading._TimerCallback
0:000> ~290e !clrstack
OS Thread Id: 0x1af8 (290)
ESP       EIP     
2e77f39c 77d2013d [HelperMethodFrame_1OBJ: 2e77f39c] System.Threading.ReaderWriterLock.AcquireReaderLockInternal(Int32)
2e77f3f8 0c3630d4 MyProg.MyEngine.GetBuyValue()
2e77f424 076fd4ad MyProg.MyParentClass.Execute(System.Object)
2e77f474 71e19fcf System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(System.Object)
2e77f47c 71e302ff System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
2e77f494 71e1a533 System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(System.Threading._ThreadPoolWaitCallback)
2e77f4a8 71e1a3c9 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(System.Object)
2e77f638 74071b4c [GCFrame: 2e77f638] 
0:000> ~290e!dso
OS Thread Id: 0x1af8 (290)
ESP/REG  Object   Name
2e77f35c 022f3014 MyProg.MyEngine
2e77f360 02369b98 System.Threading.ContextCallback
2e77f378 02369b98 System.Threading.ContextCallback
2e77f3dc 023188f8 System.Threading.ReaderWriterLock
2e77f400 024862d8 MyProg.MyChildClass
2e77f410 02369b98 System.Threading.ContextCallback
2e77f430 024862d8 MyProg.MyChildClass
2e77f43c 024862d8 MyProg.MyChildClass
2e77f460 02369b98 System.Threading.ContextCallback
2e77f490 1156dd74 System.Threading._ThreadPoolWaitCallback

解决方法:

我认为您应该尝试使用此家伙http://stevestechspot.com/编写的扩展名DLL SOSEX,这里有一个!dlk命令试图查找死锁,他最近添加了一些新的命令,这些命令对您来说很有趣:

!mlocks – Lists all managed locks and unmanaged CriticalSections,
along with their owner thread ID. !mwaits – Lists all waiting threads
and, if it can be determined, the lock objects they are waiting on.

标签:windbg,c,hung
来源: https://codeday.me/bug/20191201/2079260.html