UnhandledException handler like below sounds like it could be triggered by any unhandled exception, and it tends to hang on to whatever locks it was holding so, lock order is a problem. In the case below, we encountered a deadlock between the thread pool and the timer queue. I was told in .Net Framework; we did this to clean up shared performance counters which would survive a process restart. Any reason why we need to have this handler registered for MemoryCache in .Net Core?
ntdll!ZwWaitForMultipleObjects+0x14
KERNELBASE!WaitForMultipleObjectsEx+0xf0
coreclr!Thread::DoAppropriateAptStateWait+0x44
coreclr!Thread::DoAppropriateWaitWorker+0x242
coreclr!Thread::DoAppropriateWait+0x89
coreclr!CLREventBase::WaitEx+0x53
coreclr!CLREventBase::Wait+0x59
coreclr!AwareLock::EnterEpilogHelper+0x1d5
coreclr!AwareLock::EnterEpilog+0x4a
coreclr!AwareLock::Enter+0xb2
coreclr!SyncBlock::EnterMonitor+0x8
coreclr!ObjHeader::EnterObjMonitor+0xd
coreclr!Object::EnterObjMonitor+0x16
coreclr!JIT_MonEnter_Helper+0x145
coreclr!JIT_MonReliableEnter_Portable+0xe0
System_Private_CoreLib!System.Threading.TimerQueueTimer.Close()+0xffffffff`a36c5062
System_Private_CoreLib!System.Threading.Timer.Dispose()+0xffffffff`a40634f4
System_Runtime_Caching!System.Runtime.Caching.GCHandleRef`1[[System.__Canon, System.Private.CoreLib]].Dispose()+0x45
System_Runtime_Caching!System.Runtime.Caching.MemoryCacheStatistics.Dispose()+0x81
System_Runtime_Caching!System.Runtime.Caching.MemoryCache.Dispose()+0x3b
System_Runtime_Caching!System.Runtime.Caching.MemoryCache.OnUnhandledException(System.Object, System.UnhandledExceptionEventArgs)+0x35
coreclr!CallDescrWorkerInternal+0x83
coreclr!DispatchCallSimple+0x83
coreclr!ExceptionNotifications::DeliverExceptionNotification+0x46
coreclr!InvokeUnhandledSwallowing+0xa7
coreclr!DistributeUnhandledExceptionReliably+0x159
coreclr!AppDomain::RaiseUnhandledExceptionEvent+0xc7
coreclr!AppDomain::OnUnhandledException+0x6b
coreclr!NotifyAppDomainsOfUnhandledException+0x150
coreclr!InternalUnhandledExceptionFilter_Worker+0x27d
coreclr!ThreadBaseExceptionAppDomainFilter+0x17
coreclr!ThreadBaseRedirectingFilter+0x2c
coreclr!`ManagedThreadBase_DispatchOuter'::`1'::filt$0+0x21
coreclr!__C_specific_handler+0xa0
ntdll!RtlpExecuteHandlerForException+0xf
ntdll!RtlDispatchException+0x286
ntdll!RtlRaiseException+0x195
KERNELBASE!RaiseException+0x6c
coreclr!RaiseTheExceptionInternalOnly+0x28c
coreclr!UnwindAndContinueRethrowHelperAfterCatch+0x42
coreclr!JIT_NewArr1+0x829
System_Private_CoreLib!System.Collections.Concurrent.ConcurrentQueueSegment`1[[System.__Canon, System.Private.CoreLib]]..ctor(Int32)+0xffffffff`a3ae4acd
System_Private_CoreLib!System.Collections.Concurrent.ConcurrentQueue`1[[System.__Canon, System.Private.CoreLib]].EnqueueSlow(System.__Canon)+0xc5
System_Private_CoreLib!System.Threading.ThreadPoolWorkQueue.Enqueue(System.Object, Boolean)+0xffffffff`a36fd43f
System_Private_CoreLib!System.Threading.TimerQueue.FireNextTimers()+0xffffffff`a35fdb3b
coreclr!CallDescrWorkerInternal+0x83
coreclr!CallDescrWorkerWithHandler+0x57
coreclr!MethodDescCallSite::CallTargetWorker+0x196
coreclr!MethodDescCallSite::Call+0x16
coreclr!AppDomainTimerCallback_Worker+0x39
coreclr!ManagedThreadBase_DispatchInner+0xd
coreclr!ManagedThreadBase_DispatchMiddle+0x7e
coreclr!ManagedThreadBase_DispatchOuter+0xaf
coreclr!ManagedThreadBase_FullTransition+0x23
coreclr!ManagedThreadBase::ThreadPool+0x23
coreclr!AppDomainTimerCallback+0x5f
coreclr!ThreadpoolMgr::AsyncTimerCallbackCompletion+0x38
coreclr!UnManagedPerAppDomainTPCount::DispatchWorkItem+0x15d
coreclr!ThreadpoolMgr::ExecuteWorkRequest+0x13e
coreclr!ThreadpoolMgr::WorkerThreadStart+0x500
coreclr!Thread::intermediateThreadProc+0x8a
kernel32!BaseThreadInitThunk+0x10
ntdll!RtlUserThreadStart+0x2b
ntdll!ZwWaitForMultipleObjects+0x14
KERNELBASE!WaitForMultipleObjectsEx+0xf0
coreclr!Thread::DoAppropriateAptStateWait+0x44
coreclr!Thread::DoAppropriateWaitWorker+0x242
coreclr!Thread::DoAppropriateWait+0x89
coreclr!CLREventBase::WaitEx+0x53
coreclr!CLREventBase::Wait+0x59
coreclr!AwareLock::EnterEpilogHelper+0x1d5
coreclr!AwareLock::EnterEpilog+0x4a
coreclr!AwareLock::Enter+0xb2
coreclr!SyncBlock::EnterMonitor+0x8
coreclr!ObjHeader::EnterObjMonitor+0xd
coreclr!Object::EnterObjMonitor+0x16
coreclr!JIT_MonEnter_Helper+0x145
coreclr!JIT_MonReliableEnter_Portable+0xe0
System_Private_CoreLib!System.Collections.Concurrent.ConcurrentQueue`1[[System.__Canon, System.Private.CoreLib]].EnqueueSlow(System.__Canon)+0x6c
System_Private_CoreLib!System.Threading.ThreadPoolWorkQueue.Enqueue(System.Object, Boolean)+0xffffffff`a36fd43f
System_Private_CoreLib!System.Threading.TimerQueue.FireNextTimers()+0xffffffff`a35fdb3b
coreclr!CallDescrWorkerInternal+0x83
coreclr!CallDescrWorkerWithHandler+0x57
coreclr!MethodDescCallSite::CallTargetWorker+0x196
coreclr!MethodDescCallSite::Call+0x16
coreclr!AppDomainTimerCallback_Worker+0x39
coreclr!ManagedThreadBase_DispatchInner+0xd
coreclr!ManagedThreadBase_DispatchMiddle+0x7e
coreclr!ManagedThreadBase_DispatchOuter+0xaf
coreclr!ManagedThreadBase_FullTransition+0x23
coreclr!ManagedThreadBase::ThreadPool+0x23
coreclr!AppDomainTimerCallback+0x5f
coreclr!ThreadpoolMgr::AsyncTimerCallbackCompletion+0x38
coreclr!UnManagedPerAppDomainTPCount::DispatchWorkItem+0x15d
coreclr!ThreadpoolMgr::ExecuteWorkRequest+0x13e
coreclr!ThreadpoolMgr::WorkerThreadStart+0x500
coreclr!Thread::intermediateThreadProc+0x8a
kernel32!BaseThreadInitThunk+0x10
ntdll!RtlUserThreadStart+0x2b
UnhandledException handler like below sounds like it could be triggered by any unhandled exception, and it tends to hang on to whatever locks it was holding so, lock order is a problem. In the case below, we encountered a deadlock between the thread pool and the timer queue. I was told in .Net Framework; we did this to clean up shared performance counters which would survive a process restart. Any reason why we need to have this handler registered for MemoryCache in .Net Core?