主线程等待自己?

时间:2015-05-07 06:18:35

标签: .net wpf windbg hang finalizer

我在.NET应用程序中有异常情况我有问题需要理解。用户声明应用程序挂起,我们收到了转储文件。 DebugDiag显示一个阻塞的终结器线程。当然这是一个问题,但它不应该导致挂起。那时,据我所知,只有垃圾收集应该停止工作和内存增加,但应用程序不应该挂起。

终结器线程上的堆栈跟踪显示如下,我们可以看到终结器正在等待某个事件,但我没有相关信息。

ChildEBP RetAddr  Args to Child              
05ebf2dc 75a314ab **000003e0** 00000000 00000000 ntdll!ZwWaitForSingleObject+0x15
05ebf348 75ed1194 000003e0 ffffffff 00000000 KERNELBASE!WaitForSingleObjectEx+0x98
05ebf360 75ed1148 000003e0 ffffffff 00000000 kernel32!WaitForSingleObjectExImplementation+0x75
05ebf374 76617690 000003e0 ffffffff 0072a0d8 kernel32!WaitForSingleObject+0x12
05ebf398 7673a4d1 00729b10 0071bee0 05ebf4a4 ole32!GetToSTA+0xad [d:\w7rtm\com\ole32\com\dcomrem\chancont.cxx @ 133]
05ebf3c8 7673cef0 05ebf490 05ebf5b8 0072a0d8 ole32!CRpcChannelBuffer::SwitchAptAndDispatchCall+0x140 [d:\w7rtm\com\ole32\com\dcomrem\channelb.cxx @ 4419]
05ebf4a8 76639d01 0072a0d8 05ebf5b8 05ebf5a0 ole32!CRpcChannelBuffer::SendReceive2+0xef [d:\w7rtm\com\ole32\com\dcomrem\channelb.cxx @ 4076]
05ebf524 7661792b 0072a0d8 05ebf5b8 05ebf5a0 ole32!CAptRpcChnl::SendReceive+0xaf [d:\w7rtm\com\ole32\com\dcomrem\callctrl.cxx @ 603]
05ebf578 7673ce06 00000001 080447c8 05ebf5a0 ole32!CCtxComChnl::SendReceive+0x95 [d:\w7rtm\com\ole32\com\dcomrem\ctxchnl.cxx @ 679]
05ebf594 75bc421b 08007644 05ebf5e4 75c40149 ole32!NdrExtpProxySendReceive+0x49 [d:\w7rtm\com\rpc\ndrole\proxy.cxx @ 1932]
05ebf5a0 75c40149 05cd51ea 05ebf9ec 0700022b rpcrt4!NdrpProxySendReceive+0xe
05ebf9b4 7673c8e2 7664fa10 766549b8 05ebf9ec rpcrt4!NdrClientCall2+0x1a6
05ebf9d4 766398ad 0000000c 00000008 05ebfa6c ole32!ObjectStublessClient+0xa2 [d:\w7rtm\com\rpc\ndrole\i386\stblsclt.cxx @ 474]
05ebf9e4 7661805c 08007644 05ebfa20 00000000 ole32!ObjectStubless+0xf [d:\w7rtm\com\rpc\ndrole\i386\stubless.asm @ 154]
05ebfa6c 7660d4b4 006f1698 6dc5dea2 05ebfb2c ole32!CObjectContext::InternalContextCallback+0x128 [d:\w7rtm\com\ole32\com\dcomrem\context.cxx @ 4299]
05ebfabc 6dc5e760 006f16a8 6dc5dea2 05ebfb2c ole32!CObjectContext::ContextCallback+0x92 [d:\w7rtm\com\ole32\com\dcomrem\context.cxx @ 4184]
05ebfbbc 6dc5e838 6dbfef7f 05ebfc4c 05333247 clr!CtxEntry::EnterContext+0x25a
05ebfbf4 6dbff03d 6dbfef7f 05ebfc4c 07ff9740 clr!RCW::EnterContext+0x40
05ebfc10 6dc5e63a 006f1808 05333593 6e19de08 clr!RCWCleanupList::ReleaseRCWListInCorrectCtx+0xc0
05ebfc6c 6dc02e9b 0533350f 00000001 00000000 clr!RCWCleanupList::CleanupAllWrappers+0x155
05ebfcbc 6dc02c3b 6e1a0d88 00000004 05333523 clr!SyncBlockCache::CleanupSyncBlocks+0xd0
05ebfcd4 6dc05a62 05333553 05ebfdf8 05ebfe70 clr!Thread::DoExtraWorkForFinalizer+0x81
05ebfd14 6db912d5 05ebfd34 6dbd82f7 05ebfe70 clr!WKS::GCHeap::FinalizerThreadWorker+0x22f
05ebfda4 6db9149b 6dc43bec 05ebfdf4 6db91528 clr!REGUTIL::EnvGetString+0xfc
05ebfdb0 6db91528 6dc43bec 006dcd70 05ebfd4c clr!SHash<StringSHashTraits<_ConfigStringKeyValuePair,unsigned short,CaseSensitiveStringCompareHash<unsigned short> > >::Lookup+0x11
05ebfe38 6dc4b5a1 05333707 6dc4b43b 00000000 clr!EEConfig::GetConfiguration_DontUse_+0x1b0
05ebfeb4 6dcf36f8 00000000 00000000 00000000 clr!WKS::GCHeap::FinalizerThreadStart+0x198
05ebff58 75ed338a 006c3d50 05ebffa4 77bcbf32 clr!Thread::intermediateThreadProc+0x4d
05ebff64 77bcbf32 006c3d50 722088ec 00000000 kernel32!BaseThreadInitThunk+0xe
05ebffa4 77bcbf05 6dcf36af 006c3d50 00000000 ntdll!__RtlUserThreadStart+0x70
05ebffbc 00000000 6dcf36af 006c3d50 00000000 ntdll!_RtlUserThreadStart+0x1b
0:002> !handle **000003e0**
Handle 000003e0
  Type          Event
0:002> !handle 000003e0 ff
Handle 000003e0
  Type          Event
  Attributes    0
  GrantedAccess 0x1f0003:
         Delete,ReadControl,WriteDac,WriteOwner,Synch
         QueryState,ModifyState
  HandleCount   2
  PointerCount  4
  Name          <none>
  Object specific information

当我检查主线程时,我得到堆栈跟踪,显示它在WaitForMultipleObjects中等待。它只有一个要等待的对象,它是一个线程的句柄。奇怪的是,这是主线程本身的处理。堆栈跟踪如下:

0:000> kb
ChildEBP RetAddr  Args to Child              
002e0b5c 75a315f7 **00000001** **002e0bac** 00000001 ntdll!ZwWaitForMultipleObjects+0x15
002e0bf8 75ed19f8 002e0bac 002e0c20 00000000 KERNELBASE!WaitForMultipleObjectsEx+0x100
002e0c40 72ff1c98 00000001 7efde000 00000000 kernel32!WaitForMultipleObjectsExImplementation+0xe0
002e0c90 72fe49f2 00000000 00000001 00000000 WindowsBase_ni+0xe1c98
002e0ca8 72fcb91d 00000000 00000001 00000000 WindowsBase_ni+0xd49f2
002e0cc0 6cebcf5a 00000001 00000000 002e0ce4 WindowsBase_ni+0xbb91d
002e0cd0 6db63de2 00000001 00000000 006d0bd8 mscorlib_ni+0x38cf5a
002e0ce4 6db73315 002e0d8c 002e0d28 6dcb2c66 clr!CallDescrWorkerInternal+0x34
002e0d38 6db76cdf 002e0e28 00000000 00000004 clr!CallDescrWorkerWithHandler+0x6b
002e0dc0 6dc0d8d4 002e0e5c 00f6c713 00000001 clr!MethodDescCallSite::CallTargetWorker+0x152
002e0ea0 6dbf0a64 002e0ed8 00000001 002e0fd8 clr!Thread::DoSyncContextWait+0xb4
002e0f30 6dccc90c 00000001 002e0fd8 00000000 clr!Thread::DoAppropriateWaitWorker+0x100
002e0f9c 6dc5ea37 00000001 002e0fd8 00000000 clr!Thread::DoAppropriateWait+0x64
002e0fec 6dc5eae1 00000001 006d0bd8 00f6ddd3 clr!Thread::JoinEx+0xc2
002e1460 6dc16962 1996a760 00000ebd 6ceff040 clr!RCW::Initialize+0x3ba
002e14a4 6dc169e9 00000000 6ceff040 00f6dd6f clr!RCW::CreateRCWInternal+0xd6
002e14dc 6dc1656d 00000000 6ceff040 00f6dcdb clr!RCW::CreateRCW+0x2b
002e1568 6dc16c4e 00000000 002e1594 002e16bc clr!COMInterfaceMarshaler::CreateObjectRef+0xb7
002e1630 6dc15a78 002e16bc 72fff7c0 00000000 clr!COMInterfaceMarshaler::FindOrCreateObjectRefInternal+0x272
002e1b00 6dc15c6f 00000000 72fff7c0 00000000 clr!GetObjectRefFromComIP+0x40b
002e1b20 6dc15bcc 72fff7c0 00000000 00000000 clr!UnmarshalObjectFromInterface+0x3c
002e1bc4 73147126 00000000 00000000 6b212854 clr!StubHelpers::InterfaceMarshaler__ConvertToManaged+0xeb
002e1bf8 6db6421e 199bbe40 96a76000 002e1e18 WindowsBase_ni+0x237126
002e1c20 6dbf6fbf 73147100 6b212854 002e1cb4 clr!COMToCLRDispatchHelper+0x6b
002e1c8c 76392d7f 1996a820 1996a760 00000000 clr!COMToCLRWorker+0x3e6
002e1cd8 76393ce0 00000001 00000002 1996a77c msctf!CInputContext::_NotifyEndEdit+0x13b
002e1ce8 76392c61 00000002 002e1d2c 00000002 msctf!CInputContext::_SynchAppChanges+0x76
002e1d00 76392c21 1996a77c 00000002 00000000 msctf!CInputContext::OnLockGranted+0x3d
002e1d18 7314b96f 199b6c24 00000002 00da93b2 msctf!CACPWrap::OnLockGranted+0x7d
002e1d80 6bbd30bd 1ae292ec 1ae292ec 002e1dd0 WindowsBase_ni+0x23b96f
002e1d90 6bbd2ef8 170f8f78 170f8f14 1ae292ec PresentationFramework_ni+0xd330bd
002e1dd0 6b21291a 00000008 00000000 0c77c468 PresentationFramework_ni+0xd32ef8
002e1de4 731534ba 002e1f08 6b2128b4 002e1f4c PresentationFramework_ni+0x37291a
002e1e0c 6db6421e 002e1f08 002ef1d8 6dcbff59 WindowsBase_ni+0x2434ba
002e1e30 6dbf6fbf 731534a4 6b2128b4 002e1ec8 clr!COMToCLRDispatchHelper+0x6b
002e1ea0 76392e3d 002e1f34 199b6c20 1b14978c clr!COMToCLRWorker+0x3e6
002e1ed4 76392e11 199b6c20 00000002 002e1f08 msctf!CACPWrap::RequestLock+0x17
002e1ef0 763a1cd4 199b6c20 00000002 002e1f08 msctf!SafeRequestLock+0x1c
002e1f0c 763a1c94 00000001 002e1f24 763a1c6c msctf!CInputContext::_OnSelectionChangeInternal+0x3a
002e1f18 763a1c6c 1996a77c 002e1f84 72fec378 msctf!CInputContext::OnSelectionChange+0x22
002e1f24 72fec378 199b6c24 00da93b2 6db6abc8 msctf!CACPWrap::OnSelectionChange+0x1e
002e1f84 6bbd2374 002e1fa0 6bde4a15 1b14978c WindowsBase_ni+0xdc378

当我在WaitForMultipleObjects(只有一个)中检查对象时,我得到:

0:000> dd **002e0bac**
002e0bac  **000000d8** 00000000 03cd88e4 002e0bd0
002e0bbc  72fcb91d 00000000 00000001 00000000
002e0bcc  ffffd8f0 ffffffff 6cebcf5a 00000001
002e0bdc  00000000 002e0b78 6db63de2 002e0cf0
002e0bec  75a57000 75871304 00000000 002e0c40
002e0bfc  75ed19f8 002e0bac 002e0c20 00000000
002e0c0c  00000001 00000000 006d0bd8 1b149bc8
002e0c1c  00000000 000000d8 002e0c9c 00000000
0:000> !handle **000000d8** ff
Handle 000000d8
  Type          Thread
  Attributes    0
  GrantedAccess 0x1fffff:
         Delete,ReadControl,WriteDac,WriteOwner,Synch
         Terminate,Suspend,Alert,GetContext,SetContext,SetInfo,QueryInfo,SetToken,Impersonate,DirectImpersonate
  HandleCount   6
  PointerCount  12
  Name          <none>
  Object specific information
    Thread Id   **fc8.ef0**
    Priority    10
    Base Priority 0

线程枚举如下:

0:000> !threads
ThreadCount:      55
UnstartedThread:  0
BackgroundThread: 14
PendingThread:    0
DeadThread:       38
Hosted Runtime:   no
                                                                         Lock  
       ID OSID ThreadOBJ    State GC Mode     GC Alloc Context  Domain   Count Apt Exception
   0    1  **ef0** 006d0bd8     26020 Preemptive  1B149BD0:00000000 006c85d0 0     STA 
   2    2  eec 006dcd70     2b220 Preemptive  00000000:00000000 006c85d0 0     MTA (Finalizer) 
XXXX    5    0 080ae2d8     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
   5    6  98c 0c7444e8   a029220 Preemptive  00000000:00000000 006c85d0 0     MTA (Threadpool Completion Port) 
   6   10  664 0c751228   202b020 Preemptive  1B08EE68:00000000 006c85d0 0     MTA 
   7    4  8f8 08099f50   102a220 Preemptive  00000000:00000000 006c85d0 0     MTA (Threadpool Worker) 
   8   11  da8 0c7bbf30     27220 Preemptive  1B0C2E50:00000000 006c85d0 0     STA 
XXXX   12    0 0c7dbe08     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   14    0 0c7eef70   1039820 Preemptive  00000000:00000000 006c85d0 0     STA (Threadpool Worker) 
  10   15 13b8 0c7f2350   1029220 Preemptive  1B0C93FC:00000000 006c85d0 0     MTA (Threadpool Worker) 
XXXX   16    0 0c7f3488   1039820 Preemptive  00000000:00000000 006c85d0 0     Ukn (Threadpool Worker) 
  11   17  894 0c7ff088   1029220 Preemptive  1B1463C4:00000000 006c85d0 0     MTA (Threadpool Worker) 
XXXX   19    0 07fd7ab8     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   23    0 07fd8fd8     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   24    0 07fd9520     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   25    0 07fd9a68     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   28    0 07fdaa40     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   29    0 07fdaf88     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
  12   20 1178 07fd8000   1029220 Preemptive  00000000:00000000 006c85d0 0     MTA (Threadpool Worker) 
XXXX   27    0 07fda4f8     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   26    0 07fd9fb0     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX    8    0 0c7c3618     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
  14   31 149c 0c7c40a8   1029220 Preemptive  00000000:00000000 006c85d0 0     MTA (Threadpool Worker) 
XXXX    7    0 0c7c30d0     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   32    0 0c7c45f0     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   34    0 0c7c5080     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   33    0 0c7c4b38     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   36    0 0c7c5b10     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   37    0 0c7c6058     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
  15   51 1504 129a2180   1029220 Preemptive  1B09C2A4:00000000 006c85d0 0     MTA (Threadpool Worker) 
  16   53 150c 0c7a8c88   1029220 Preemptive  1B120744:00000000 006c85d0 0     MTA (Threadpool Worker) 
  17   45 1548 129a01d0   1029220 Preemptive  1B11F2A0:00000000 006c85d0 0     MTA (Threadpool Worker) 
  18   44 154c 1299fc88   1029220 Preemptive  1B1482F0:00000000 006c85d0 0     MTA (Threadpool Worker) 
  19   43 157c 1299f740   1029220 Preemptive  00000000:00000000 006c85d0 0     MTA (Threadpool Worker) 
XXXX   49    0 129a1c38     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   46    0 0c7a9718     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   38    0 0c7abc10     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
  20   22 1100 0c7a9c60   8029220 Preemptive  00000000:00000000 006c85d0 0     MTA (Threadpool Completion Port) 
XXXX   40    0 0c7aa6f0     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   48    0 0c7aa1a8     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   39    0 0c7a8740     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   13    0 0c7ac158     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   42    0 129a0718     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   18    0 19932698     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   35    0 19932be0     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX    9    0 19933128     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   41    0 19934100     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   47    0 19933bb8     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX    3    0 129a16f0     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   55    0 0c7c65a0     39820 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   68    0 07fd8a90     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
  34   76  740 07fd8548   202b020 Preemptive  00000000:00000000 006c85d0 0     MTA 
XXXX   75    0 07fd7570     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   74    0 1299e768     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 
XXXX   73    0 1299f1f8     39820 Preemptive  00000000:00000000 006c85d0 0     Ukn 

只是提到锁是可以的,没有死锁。那么问题是这是如何可能的?线程如何等待自己以及如何解决?或者我怎样才能在终结器中获取事件句柄的对象特定信息(为什么这是空的,谁创建了该句柄)?

解决阻止的终结器是我的第一次尝试,我希望看到终结器正在等待一些线程打开,但我得到了这种情况。因此,阻塞的终结器很可能只是主线程被阻塞的结果。同样,我不希望发布解决方案,而是由对线程模型和Windows内部知识有更多了解的人解释这种情况。提前致谢。

0 个答案:

没有答案