title: How to determine the cause of garbage collection-induced deadlock?
body: <p>I'm trying to debug what I believe to be a deadlock caused by garbage collection in a service written in .net.
  I've managed to create a dump file from when the application has entered the deadlock, but I'm not quite sure how to decide root cause using WinDbg.</p>
  
  <p>When loading the dump i started by executing !locks:</p>
  
  <pre><code>0:000&gt; !locks
  
  CritSec +28bbecf0 at 0000019f28bbecf0
  WaiterWoken        No
  LockCount          0
  RecursionCount     1
  OwningThread       3350
  EntryCount         0
  ContentionCount    1c
  *** Locked
  
  CritSec +43810c00 at 0000019f43810c00
  WaiterWoken        No
  LockCount          18
  RecursionCount     1
  OwningThread       4bd4
  EntryCount         0
  ContentionCount    12
  *** Locked
  
  Scanned 32 critical sections
  </code></pre>
  
  <p>Looking at !threads, this is the (abbreviated to relevant threads from !locks) output where one thread seems to be part of garbage collection(?):</p>
  
  <pre><code>0:000&gt; !threads
  ThreadCount:      55
  UnstartedThread:  0
  BackgroundThread: 12
  PendingThread:    0
  DeadThread:       0
  Hosted Runtime:   no
  
                                                                                                          Lock  
         ID OSID ThreadOBJ           State GC Mode     GC Alloc Context                  Domain           Count Apt Exception
    41   37 3350 0000019f443794a0    2b028 Preemptive  0000019F2AB55FA0:0000019F2AB55FD0 0000019f28bdf720 3     MTA (GC) 
    17   16 4bd4 0000019f436928d0    ab028 Preemptive  0000019F2A8FDAA0:0000019F2A8FED28 0000019f28bdf720 3     MTA System.Reflection.TargetInvocationException 0000019f2a7b5800
  </code></pre>
  
  <p>Right, looking at individual threads:</p>
  
  <pre><code>0:000&gt; ~41e !clrstack
  OS Thread Id: 0x3350 (41)
          Child SP               IP Call Site
  000000667c83cfa8 00007ffde29c7c84 [HelperMethodFrame: 000000667c83cfa8] 
  000000667c83d120 00007ffdb8325b74 System.RuntimeType.GetEnumNames()
  000000667c83d160 00007ffd71e6e0a4 System.Data.Linq.SqlClient.SqlTypeSystem+ProviderBase.Parse(System.String)
  000000667c83d1d0 00007ffd71e6dd00 System.Data.Linq.SqlClient.SqlFactory.Member(System.Data.Linq.SqlClient.SqlExpression, System.Data.Linq.Mapping.MetaDataMember)
  000000667c83d220 00007ffd71e6db17 System.Data.Linq.SqlClient.Translator.BuildProjectionInternal(System.Data.Linq.SqlClient.SqlExpression, System.Data.Linq.Mapping.MetaType, System.Collections.Generic.IEnumerable`1, Boolean, System.Data.Linq.SqlClient.SqlLink, System.Linq.Expressions.Expression)
  000000667c83d2a0 00007ffd71e6d3ad System.Data.Linq.SqlClient.Translator.BuildProjection(System.Data.Linq.SqlClient.SqlExpression, System.Data.Linq.Mapping.MetaType, Boolean, System.Data.Linq.SqlClient.SqlLink, System.Linq.Expressions.Expression)
  000000667c83d380 00007ffd71e6ca4f System.Data.Linq.SqlClient.Translator.BuildDefaultQuery(System.Data.Linq.Mapping.MetaType, Boolean, System.Data.Linq.SqlClient.SqlLink, System.Linq.Expressions.Expression)
  000000667c83d400 00007ffd71e6c835 System.Data.Linq.SqlClient.QueryConverter.TranslateConstantTable(System.Data.Linq.ITable, System.Data.Linq.SqlClient.SqlLink)
  000000667c83d450 00007ffd71e6c4bd System.Data.Linq.SqlClient.QueryConverter.CoerceToSequence(System.Data.Linq.SqlClient.SqlNode)
  000000667c83d4a0 00007ffd71e6ba4e System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression)
  000000667c83d4f0 00007ffd71e66257 System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression)
  000000667c83d550 00007ffd71e6b556 System.Data.Linq.SqlClient.QueryConverter.VisitSelect(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression)
  000000667c83d5f0 00007ffd71e66257 System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression)
  000000667c83d650 00007ffd71e6aefe System.Data.Linq.SqlClient.QueryConverter.VisitFirst(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, Boolean)
  000000667c83d6b0 00007ffd71e66257 System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression)
  000000667c83d710 00007ffd71e65993 System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(System.Linq.Expressions.Expression)
  000000667c83d750 00007ffd71e654b7 System.Data.Linq.SqlClient.SqlProvider.BuildQuery(System.Linq.Expressions.Expression, System.Data.Linq.SqlClient.SqlNodeAnnotations)
  000000667c83d7b0 00007ffd71e6059a System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(System.Linq.Expressions.Expression)
  000000667c83d820 00007ffd71e5fbe1 System.Data.Linq.DataQuery`1[[System.__Canon, mscorlib]].System.Linq.IQueryProvider.Execute[[System.__Canon, mscorlib]](System.Linq.Expressions.Expression)
  
  *Library code here*
  
  000000667c83e348 00007ffdd1426573 [DebuggerU2MCatchHandlerFrame: 000000667c83e348] 
  000000667c83e658 00007ffdd1426573 [HelperMethodFrame_PROTECTOBJ: 000000667c83e658] System.RuntimeMethodHandle.InvokeMethod(System.Object, System.Object[], System.Signature, Boolean)
  000000667c83e7d0 00007ffdb83e5230 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(System.Object, System.Object[], System.Object[])
  000000667c83e840 00007ffdb832ff29 System.Delegate.DynamicInvokeImpl(System.Object[])
  
  *Library code here*
  
  000000667c83f010 00007ffdb833674e System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
  000000667c83f0e0 00007ffdb83365e7 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
  000000667c83f110 00007ffdb83365a2 System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
  000000667c83f160 00007ffdb833d1f2 System.Threading.ThreadHelper.ThreadStart()
  000000667c83f3b8 00007ffdd1426573 [GCFrame: 000000667c83f3b8] 
  000000667c83f708 00007ffdd1426573 [DebuggerU2MCatchHandlerFrame: 000000667c83f708] 
  
  
  0:000&gt; ~17e !clrstack
  OS Thread Id: 0x4bd4 (17)
          Child SP               IP Call Site
  000000667aff7f40 00007ffde29c6154 [PrestubMethodFrame: 000000667aff7f40] MongoDB.Driver.MongoClientSettings.GetHashCode()
  000000667aff8200 00007ffd7209b420 MongoDB.Driver.MongoClientSettings.Freeze()
  000000667aff8230 00007ffd7209b137 MongoDB.Driver.MongoClient..ctor(MongoDB.Driver.MongoClientSettings)
  000000667aff8270 00007ffd720975b4 Foo.Bar.You.Cannot.See.This..cctor()
  000000667aff8700 00007ffdd1426573 [GCFrame: 000000667aff8700] 
  000000667aff9380 00007ffdd1426573 [PrestubMethodFrame: 000000667aff9380] Foo.Bar.You.Cannot.See.This..ctor()
  </code></pre>
  
  <p>Noteworthy may be that the MongoClient..ctor is a static readonly field.</p>
  
  <p>Also, !runaway seems to indicate these two threads are somehow the culprit:</p>
  
  <pre><code>0:000&gt; !runaway
   User Mode Time
    Thread       Time
    41:3350      0 days 0:03:29.140
    17:4bd4      0 days 0:00:02.468
  </code></pre>
  
  <p>Using sosex, !syncblck also points at thread 41:</p>
  
  <pre><code>0:000&gt; !syncblk
  Index SyncBlock MonitorHeld Recursion Owning Thread Info  SyncBlock Owner
     23 0000019f42f09168           27         1 0000019f443794a0 3350  41   0000019f2a7180c0 System.Object
  -----------------------------
  Total           59
  CCW             4
  RCW             0
  ComClassFactory 0
  Free            0
  </code></pre>
  
  <p>!dlk comes up empty though.</p>
  
  <p>Using !dumpstack on the threads we see some indications that a garbage collection is happening, and a thread is being suspended:</p>
  
  <pre><code>0:000&gt; ~41e !dumpstack
  OS Thread Id: 0x3350 (41)
  Current frame: ntdll!NtFlushProcessWriteBuffers+0x14
  Child-SP         RetAddr          Caller, Callee
  000000667c83c9a0 00007ffdd1501724 clr!ThreadSuspend::SuspendRuntime+0xd4, calling ntdll!NtFlushProcessWriteBuffers
  000000667c83c9c0 00007ffde2950c10 ntdll!RtlEnterCriticalSection+0x40, calling ntdll!RtlpEnterCriticalSectionContended
  000000667c83c9d0 00007ffdd18447ed clr!CoTemplate_h+0x65, calling clr!_security_check_cookie
  000000667c83c9e0 00007ffdd14fdcad clr!ThreadStore::TrapReturningThreads+0x6d, calling clr!GetThread
  000000667c83c9f0 00007ffdd1424fe1 clr!ClrFlsIncrementValue+0x29
  000000667c83ca20 00007ffdd1424ec6 clr!CrstBase::Enter+0x6a, calling ntdll!RtlTryEnterCriticalSection
  000000667c83ca30 00007ffdd1425163 clr!ClrFlsGetValue+0x23
  000000667c83ca40 00007ffdd14fdcad clr!ThreadStore::TrapReturningThreads+0x6d, calling clr!GetThread
  000000667c83ca90 00007ffdd1501608 clr!ThreadSuspend::SuspendEE+0x128, calling clr!ThreadSuspend::SuspendRuntime
  000000667c83cac0 00007ffd9629dfe6 (MethodDesc 00007ffd96064d68 +0x76 System.Linq.Expressions.Expression`1[[System.__Canon, mscorlib]]..ctor(System.Linq.Expressions.Expression, System.String, Boolean, System.Collections.ObjectModel.ReadOnlyCollection`1&lt;System.Linq.Expressions.ParameterExpression&gt;)), calling clr!JIT_WriteBarrier
  000000667c83caf0 00007ffdd15041fc clr!WKS::gc_heap::allocate_small+0x2b3, calling clr!memset
  000000667c83cb00 00007ffddfa248fa KERNELBASE!ResetEvent+0xa, calling ntdll!NtClearEvent
  000000667c83cb20 00007ffd962bd86f (MethodDesc 00007ffd96064d98 +0x3f System.Linq.Expressions.Expression`1[[System.__Canon, mscorlib]].Create(System.Linq.Expressions.Expression, System.String, Boolean, System.Collections.ObjectModel.ReadOnlyCollection`1&lt;System.Linq.Expressions.ParameterExpression&gt;)), calling (MethodDesc 00007ffd96064d68 +0 System.Linq.Expressions.Expression`1[[System.__Canon, mscorlib]]..ctor(System.Linq.Expressions.Expression, System.String, Boolean, System.Collections.ObjectModel.ReadOnlyCollection`1&lt;System.Linq.Expressions.ParameterExpression&gt;))
  000000667c83cb30 00007ffdd14fd139 clr!CLREventBase::Reset+0x29, calling kernel32!ResetEvent
  000000667c83cb90 00007ffdd1504a29 clr!WKS::GCHeap::GarbageCollectGeneration+0xb9, calling clr!ThreadSuspend::SuspendEE
  000000667c83cbb0 00007ffdd142c39f clr!CExecutionEngine::ClrVirtualAlloc+0x4f, calling kernel32!VirtualAllocStub
  000000667c83cbf0 00007ffdd150a51e clr!AllocateArrayEx+0x4e5, calling clr!WKS::GCHeap::GarbageCollectGeneration
  000000667c83cc20 00007ffdd150cc60 clr!WKS::gc_heap::grow_heap_segment+0xe4, calling clr!StressLog::LogOn
  000000667c83ccd0 00007ffddfa10d3e KERNELBASE!LCMapStringEx+0x2de, calling KERNELBASE!_security_check_cookie
  000000667c83cd50 00007ffdd15818d8 clr!JIT_NewArr1+0x218, calling clr!AllocateArrayEx
  000000667c83cd90 00007ffdd1580f2c clr!AllocateObject+0xbc
  000000667c83cdc0 00007ffdd14d1d04 clr!MethodTable::EnsureInstanceActive+0xe0, calling clr!DomainFile::EnsureLoadLevel
  000000667c83cde0 00007ffdd1587a5e clr!MethodTable::CheckRunClassInitAsIfConstructingThrowing+0x1f, calling clr!MethodTable::GetClass
  000000667c83ce10 00007ffdd1586b76 clr!MethodTable::Allocate+0x2e, calling clr!MethodTable::CheckRunClassInitAsIfConstructingThrowing
  000000667c83ce40 00007ffdd159d7ae clr!MethodTable::FastBox+0x3e, calling clr!CopyValueClassUnchecked
  000000667c83ce70 00007ffdd15a6a57 clr!MethodTable::Box+0x3f, calling clr!MethodTable::FastBox
  000000667c83cee0 00007ffdd15a7504 clr!ReflectionEnum::InternalBoxEnum+0xd4, calling clr!HelperMethodFrameRestoreState
  000000667c83cf50 00007ffdd14b75d3 clr!RuntimeTypeHandle::IsGenericVariable+0x24, calling clr!TypeHandle::IsGenericVariable
  000000667c83cf90 00007ffdd15e84ac clr!FC_GCPoll+0x2c, calling clr!GetThread
  000000667c83cfd0 00007ffdb8325b74 (MethodDesc 00007ffdb7fa4200 +0x44 System.RuntimeType.GetEnumNames()), calling clr!JIT_NewArr1OBJ_MP_InlineGetThread
  000000667c83d068 00007ffdd1581735 clr!JIT_NewArr1+0x75, calling clr!LazyMachStateCaptureState
  000000667c83d080 00007ffdb83328e5 (MethodDesc 00007ffdb80d60b0 +0x55 System.Collections.Generic.GenericEqualityComparer`1[[System.__Canon, mscorlib]].Equals(System.__Canon, System.__Canon))
  000000667c83d0b0 00007ffdb8323e99 (MethodDesc 00007ffdb7fa5010 +0x39 System.Enum.GetCachedValuesAndNames(System.RuntimeType, Boolean)), calling clr!JIT_IsInstanceOfClass
  000000667c83d110 00007ffdb8325b74 (MethodDesc 00007ffdb7fa4200 +0x44 System.RuntimeType.GetEnumNames()), calling clr!JIT_NewArr1OBJ_MP_InlineGetThread
  000000667c83d150 00007ffd71e6e0a4 (MethodDesc 00007ffd72012018 +0x214 System.Data.Linq.SqlClient.SqlTypeSystem+ProviderBase.Parse(System.String)), calling (MethodDesc 00007ffdb7fa51d0 +0 System.Enum.GetNames(System.Type))
  </code></pre>
  
  <p>And for thread 17:</p>
  
  <pre><code>0:000&gt; ~17e !dumpstack
  OS Thread Id: 0x4bd4 (17)
  Current frame: ntdll!NtWaitForSingleObject+0x14
  Child-SP         RetAddr          Caller, Callee
  000000667aff4e40 00007ffddf9e75ff KERNELBASE!WaitForSingleObjectEx+0x8f, calling ntdll!NtWaitForSingleObject
  000000667aff4ee0 00007ffdd14fd547 clr!SVR::gc_heap::wait_for_gc_done+0x134, calling kernel32!WaitForSingleObjectEx
  000000667aff4f10 00007ffdd1451452 clr!ClassLoader::CreateTypeHandleForTypeDefThrowing+0x542, calling clr!_security_check_cookie
  000000667aff4f20 00007ffdd14fd4ff clr!SVR::gc_heap::wait_for_gc_done+0xcb, calling clr!SVR::gc_heap::wait_for_gc_done+0xf8
  000000667aff4f70 00007ffdd142f417 clr!SigPointer::GetTypeHandleThrowing+0x4b3, calling clr!_security_check_cookie
  000000667aff4f80 00007ffdd14fd4c0 clr!CLREventBase::WaitEx+0x7c, calling clr!SVR::gc_heap::wait_for_gc_done+0xac
  000000667aff4f90 00007ffde294fa87 ntdll!RtlpWaitOnAddressWithTimeout+0x43, calling ntdll!NtWaitForAlertByThreadId
  000000667aff4fc0 00007ffde294f98e ntdll!RtlpWaitOnAddress+0xb2, calling ntdll!RtlpWaitOnAddressWithTimeout
  000000667aff5010 00007ffdd1844585 clr!Thread::WaitSuspendEventsHelper+0xf5, calling clr!CLREventBase::Wait
  000000667aff5100 00007ffdd1844470 clr!Thread::WaitSuspendEvents+0x11, calling clr!Thread::WaitSuspendEventsHelper
  000000667aff5130 00007ffdd176acf6 clr! ?? ::FNODOBFM::`string'+0x3d4de, calling clr!Thread::WaitSuspendEvents
  000000667aff5170 00007ffdd15e82f3 clr!Thread::RareDisablePreemptiveGC+0x258, calling clr!Thread::RareEnablePreemptiveGC
  000000667aff5190 00007ffdd1581516 clr!CrstBase::Enter+0x10d, calling clr!CrstBase::SpinEnter
  000000667aff51a0 00007ffdd1424fe1 clr!ClrFlsIncrementValue+0x29
  000000667aff51d0 00007ffdd1425033 clr!CrstBase::Leave+0x65, calling clr!ClrFlsIncrementValue
  000000667aff5200 00007ffdd18f3492 clr!EEDbgInterfaceImpl::DisablePreemptiveGC+0x22, calling clr!Thread::RareDisablePreemptiveGC
  000000667aff5230 00007ffdd1a8ac3c clr!Debugger::LoadClass+0x15c
  000000667aff5250 00007ffdd144194b clr!MethodTable::MethodDataObject::`vector deleting destructor'+0x4b
  000000667aff5270 00007ffdd146858a clr!Module::IsVisibleToDebugger+0x23, calling clr!PEFile::IsIntrospectionOnly+0x2
  000000667aff52a0 00007ffdd19493de clr!TypeHandle::NotifyDebuggerLoad+0x86
  000000667aff52e0 00007ffdd179807e clr! ?? ::FNODOBFM::`string'+0x70db6, calling clr!TypeHandle::NotifyDebuggerLoad
  000000667aff5330 00007ffdd144855b clr!ClassLoader::DoIncrementalLoad+0x155, calling clr!ClassLoader::Notify
  000000667aff5370 00007ffdd15ec75e clr!MDInternalRW::GetSigOfMethodDef+0xae
  000000667aff5520 00007ffde298bbe8 ntdll!RtlInitializeCriticalSection+0x78, calling ntdll!_security_check_cookie
  000000667aff5530 00007ffdd142f892 clr!ClassLoader::LoadConstructedTypeThrowing+0x2ae, calling clr!MethodTable::GetLoadLevel
  000000667aff55c0 00007ffdd1448906 clr!ClassLoader::LoadTypeHandleForTypeKey_Body+0x42c, calling clr!ClassLoader::DoIncrementalLoad
  000000667aff5620 00007ffdd14334fa clr!SecurityTransparent::CheckCriticalAccess+0x49b
  000000667aff56c0 00007ffdd1441352 clr!MethodTable::GetMethodDataHelper+0x147, calling clr!GetThread
  000000667aff5700 00007ffdd14315ff clr!ClassLoader::CanAccessClass+0x8f, calling clr!SecurityTransparent::CheckCriticalAccess
  000000667aff57c0 00007ffdd1431889 clr!ClassLoader::CheckAccessMember+0x131
  000000667aff5810 00007ffdd143d605 clr!CEEInfo::getFieldInfo+0x882, calling clr!ClassLoader::CheckAccessMember
  000000667aff58d0 00007ffdd1448d96 clr!ClassLoader::LoadTypeHandleForTypeKey+0xee, calling clr!ClassLoader::LoadTypeHandleForTypeKey_Body
  000000667aff5970 00007ffdd144a546 clr!ClassLoader::LoadTypeDefThrowing+0x223, calling clr!ClassLoader::LoadTypeHandleForTypeKey
  000000667aff5a20 00007ffdd1446b83 clr!MDInternalRO::GetParentToken+0x7f
  000000667aff5a90 00007ffdd1455d9e clr!CEEInfo::resolveToken+0xc1f, calling clr!ClassLoader::LoadTypeDefThrowing
  000000667aff5b80 00007ffdd143cb1b clr!CEEInfo::isInSIMDModule+0x1c, calling clr!MethodTable::GetModule
  000000667aff5b90 00007ffdd14397e1 clr!CEEInfo::asCorInfoType+0x7e, calling clr!MethodTable::GetInternalCorElementType
  000000667aff5c10 00007ffdcdcd3108 clrjit!Compiler::verMakeTypeInfo+0xb0
  000000667aff5d70 00007ffdcdce0adc clrjit!Compiler::impReturnInstruction+0x186, calling clrjit!Compiler::impAppendStmt
  000000667aff5d90 00007ffdcdcf0e0c clrjit!Compiler::gtFoldExpr+0xc0, calling clrjit!Compiler::gtFoldExprSpecial
  000000667aff5df0 00007ffdcdd14026 clrjit!Compiler::impImportBlockCode+0x131c
  000000667aff5e20 00007ffdcdccfd28 clrjit!Compiler::impImportBlockCode+0xbc0, calling clrjit!_security_check_cookie
  000000667aff5fe0 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff60e0 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff6190 00007ffdcdcde569 clrjit!Compiler::fgMorphArgs+0x6b0, calling clrjit!_security_check_cookie
  000000667aff61c0 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff61f0 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff6220 00007ffdd1431b17 clr!ClassLoader::LoadTypeDefOrRefThrowing+0x15c, calling clr!LookupMap&lt;TypeRef * __ptr64&gt;::GetElement
  000000667aff6270 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff62a0 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff62e0 00007ffdd143cb1b clr!CEEInfo::isInSIMDModule+0x1c, calling clr!MethodTable::GetModule
  000000667aff6310 00007ffdcdd21e31 clrjit!Compiler::getBaseTypeAndSizeOfSIMDType+0x7d, calling clrjit!_security_check_cookie
  000000667aff63a0 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff63d0 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff6420 00007ffdd14454ef clr!CEEInfo::getFieldType+0x35c, calling clr!CorTypeInfo::IsObjRef
  000000667aff6450 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff6480 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff6580 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff65b0 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff6630 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff6660 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff6670 00007ffdcdcc59c6 clrjit!Compiler::fgMorphTree+0x26b, calling clrjit!_security_check_cookie
  000000667aff66f0 00007ffdcdce8ab9 clrjit!Compiler::gtSetEvalOrder+0x383, calling clrjit!Compiler::gtSetEvalOrder
  000000667aff6720 00007ffdcdccacd5 clrjit!Compiler::fgMorphSmpOp+0x750, calling clrjit!_security_check_cookie
  000000667aff6760 00007ffdd105be72 MSVCR120_CLR0400!qsort+0x3a2
  000000667aff6830 00007ffde2957a68 ntdll!RtlpAllocateHeapInternal+0xf8, calling ntdll!RtlpLowFragHeapAllocFromContext
  000000667aff6880 00007ffde2957a68 ntdll!RtlpAllocateHeapInternal+0xf8, calling ntdll!RtlpLowFragHeapAllocFromContext
  000000667aff6920 00007ffdd14267b9 clr!EEHeapFreeInProcessHeap+0x45, calling kernel32!HeapFreeStub
  000000667aff6980 00007ffdd14267b9 clr!EEHeapFreeInProcessHeap+0x45, calling kernel32!HeapFreeStub
  000000667aff69b0 00007ffdd14292c9 clr!operator delete+0x29
  000000667aff69e0 00007ffdd14647ac clr!SBuffer::ReallocateBuffer+0x7f, calling clr!operator delete
  000000667aff6a10 00007ffdd145ebfa clr!SString::Replace+0x17f, calling clr!_security_check_cookie
  000000667aff6a60 00007ffdd105bdf8 MSVCR120_CLR0400!qsort+0x328, calling MSVCR120_CLR0400!_security_check_cookie
  000000667aff6ae0 00007ffde2957a68 ntdll!RtlpAllocateHeapInternal+0xf8, calling ntdll!RtlpLowFragHeapAllocFromContext
  000000667aff6b20 00007ffdd145ec1f clr!SString::IsRepresentation+0x34, calling clr!SString::ScanASCII
  000000667aff6b40 00007ffdd1424fe1 clr!ClrFlsIncrementValue+0x29
  000000667aff6b50 00007ffdd145bf00 clr!SString::GetUnicode+0x4d, calling clr!_security_check_cookie
  000000667aff6b70 00007ffdd14310b6 clr!LoaderHeap::RealAllocMemUnsafe+0x236, calling clr!ClrFlsIncrementValue
  000000667aff6bb0 00007ffdcdcd853b clrjit!SimplerHashTable&lt;unsigned int,SmallPrimitiveKeyFuncs&lt;unsigned int&gt;,ArrayStack&lt;GenTree * __ptr64&gt; * __ptr64,DefaultSimplerHashBehavior&gt;::RemoveAll+0x53
  000000667aff6c30 00007ffdd142674a clr!EEHeapAllocInProcessHeap+0x46, calling ntdll!RtlAllocateHeap
  000000667aff6c60 00007ffdd1426804 clr!operator new+0x24
  000000667aff6c90 00007ffdcdcdba33 clrjit!Compiler::impImportBlockPending+0x143, calling clrjit!ExpandArray&lt;unsigned char&gt;::EnsureCoversInd
  000000667aff6cf0 00007ffdcdcd388e clrjit!Compiler::impImportBlock+0x90, calling clrjit!Compiler::impImportBlockCode
  000000667aff6d30 00007ffdcdcdb274 clrjit!GcInfoEncoder::Emit+0x78, calling clrjit!BitStreamWriter::CopyTo
  000000667aff6d70 00007ffdcdcdba33 clrjit!Compiler::impImportBlockPending+0x143, calling clrjit!ExpandArray&lt;unsigned char&gt;::EnsureCoversInd
  000000667aff6dd0 00007ffdcdcdb05b clrjit!Compiler::impImport+0x2ff, calling clrjit!Compiler::impImportBlock
  000000667aff6e20 00007ffdcdcd712e clrjit!Compiler::compCompile+0x7e, calling clrjit!Compiler::impImport
  000000667aff6e50 00007ffdcdced273 clrjit!Compiler::fgLinkBasicBlocks+0x7c, calling clrjit!Compiler::fgLookupBB
  000000667aff6e80 00007ffdcdcd37d0 clrjit!Compiler::fgFindBasicBlocks+0x141, calling clrjit!_security_check_cookie
  000000667aff6ec0 00007ffdcdce0dc0 clrjit!Compiler::compInitScopeLists+0xf0, calling MSVCR120_CLR0400!qsort
  000000667aff6f20 00007ffdcdcd7600 clrjit!Compiler::compCompileHelper+0x240, calling clrjit!Compiler::compCompile
  000000667aff6fb0 00007ffdcdcd739f clrjit!Compiler::compCompile+0x1af, calling clrjit!Compiler::compCompileHelper
  000000667aff7070 00007ffdcdcd352d clrjit!jitNativeCode+0x239, calling clrjit!Compiler::compCompile
  000000667aff7140 00007ffdd145ebfa clr!SString::Replace+0x17f, calling clr!_security_check_cookie
  000000667aff7160 00007ffdd149f018 clr!EECodeManager::GetFunctionSize+0x18, calling clr!GcInfoDecoder::GcInfoDecoder
  000000667aff7190 00007ffdd1433486 clr!EEJitManager::LoadJIT+0x6a, calling clr!_security_check_cookie
  000000667aff7260 00007ffdcdcd32c4 clrjit!CILJit::compileMethod+0x64, calling clrjit!jitNativeCode
  000000667aff72c0 00007ffdd143802b clr!invokeCompileMethodHelper+0xbb
  000000667aff7320 00007ffdd143464a clr!invokeCompileMethod+0x96, calling clr!invokeCompileMethodHelper
  000000667aff7360 00007ffdd143173f clr!ClassLoader::CanAccess+0xbf
  000000667aff73a0 00007ffdd143450a clr!CallCompileMethodWithSEHWrapper+0x5a, calling clr!invokeCompileMethod
  000000667aff7440 00007ffdd14343c5 clr!UnsafeJitFunction+0x5c6, calling clr!CallCompileMethodWithSEHWrapper
  000000667aff75f0 00007ffdd15de0f2 clr!PrettyPrintSigWorkerInternal+0x21a, calling clr!CQuickMemoryBase&lt;512,128&gt;::ReSizeNoThrow
  000000667aff7620 00007ffdd15de28f clr!PrettyPrintSigWorkerInternal+0x11f, calling clr!PrettyPrintSigWorkerInternal+0x200
  000000667aff7810 00007ffdd1855ef4 clr!ETW::MethodLog::SendMethodJitStartEvent+0x288, calling clr!_security_check_cookie
  000000667aff7950 00007ffdd1434a29 clr!ETW::MethodLog::MethodJitting+0x81, calling clr!CLRException::HandlerState::CleanupTry
  000000667aff79b0 00007ffdd1424ec6 clr!CrstBase::Enter+0x6a, calling ntdll!RtlTryEnterCriticalSection
  000000667aff79f0 00007ffdd14339bc clr!MethodDesc::MakeJitWorker+0x44c, calling clr!UnsafeJitFunction
  000000667aff7a20 00007ffdd1433dc8 clr!MethodDesc::MakeJitWorker+0x85f, calling clr!CrstBase::Leave
  000000667aff7bf0 00007ffdd142e7f8 clr!MethodDesc::DoPrestub+0xa5c, calling clr!MethodDesc::MakeJitWorker
  000000667aff7c20 00007ffdd142e866 clr!MethodDesc::DoPrestub+0xbf7, calling clr!MethodDesc::DoBackpatch
  000000667aff7c60 00007ffdd14b46ae clr!CExecutionEngine::AcquireLock+0x9b, calling ntdll!RtlTryEnterCriticalSection
  000000667aff7d00 00007ffdd14b3c71 clr!LockedRangeList::IsInRangeWorker+0x91, calling clr!GetThread
  000000667aff7d90 00007ffdd14fddd1 clr!Thread::RareDisablePreemptiveGC+0x71
  000000667aff7df0 00007ffdd142dceb clr!PreStubWorker+0x3cc, calling clr!MethodDesc::DoPrestub
  000000667aff8050 00007ffd962c7e91 (MethodDesc 00007ffd960614c8 +0x11 System.Linq.Enumerable+Iterator`1[[System.__Canon, mscorlib]].GetEnumerator()), calling clr!JIT_GetCurrentManagedThreadId
  000000667aff80c0 00007ffdd1421fdd clr!ResolveWorkerAsmStub+0x6d, calling clr!VSD_ResolveWorker
  000000667aff8140 00007ffdd14246b5 clr!ThePreStub+0x55, calling clr!PreStubWorker
  000000667aff81f0 00007ffd7209b420 (MethodDesc 00007ffd72113630 +0x20 MongoDB.Driver.MongoClientSettings.Freeze())
  000000667aff8220 00007ffd7209b137 (MethodDesc 00007ffd72118e00 +0x47 MongoDB.Driver.MongoClient..ctor(MongoDB.Driver.MongoClientSettings)), calling 00007ffd72096dc0 (stub for MongoDB.Driver.MongoClientSettings.Freeze())
  </code></pre>
  
  <p>I apologize for the lengthy stacks, but I wanted to be as thorough as possible when asking this question. I'm sort of at wits' end at this point, I think I've found the troublesome threads, but I'm unsure as to how to solve the deadlock. Put in a more general way: how can I, given a dump file, troubleshoot a deadlock caused by garbage collection?</p>
  
  <p>EDIT: As noted in <a href="https://blogs.msdn.microsoft.com/tess/2007/03/12/net-hang-case-study-the-gc-loader-lock-deadlock-a-story-of-mixed-mode-dlls/" rel="nofollow noreferrer">https://blogs.msdn.microsoft.com/tess/2007/03/12/net-hang-case-study-the-gc-loader-lock-deadlock-a-story-of-mixed-mode-dlls/</a>, 81% CPU usage in !threadpool indicates garbage collection, and indeed this is what's reported in my dump as well.</p>