【问题标题】:A thorny IIS deadlock caused by Autofac一个由 Autofac 引起的棘手的 IIS 死锁
【发布时间】:2013-03-08 03:30:15
【问题描述】:

我是 Autofac 用户,现在面临一个非常头疼的问题。电子邮件 Alex 和 Travis 向我提出了这个问题,并得到了他们的回复,在这里,感谢他们的帮助。 为了彻底检查这个问题,我接受了 Travis 的建议,并在此处发布了详细信息。

我的电子邮件:

我们在 mvc3 项目中使用 Autofac 2.6.3.862。

现在我们来看看IIS死锁问题很严重,服务器经常 挂了,我们用微软调试诊断工具来做 分析(导致附上分析报告),初步怀疑 成为 LifetimeScope 中的锁

我已经阅读了您的文章,但是对于一些技术细节还不清楚 我。我们的大多数 Register() 组件都是 SingleInstance,所以当 Resolve() 我不使用子生命周期范围,例如使用 (var scope = Container.BeginLifetimeScope())

,不知道这是不是死锁的原因。

调试诊断分析报告

The following threads in 
w3wp.exe__hg__PID__3904__Date__03_07_2013__Time_11_01_07AM__974__Manual Dump.dmp are waiting to enter a .NET Lock which thread 39 is currently holding    
( 26 34 42 43 44 45 46 47 48 )    
16.67% of threads blocked    

======================================================================================

Detected possible blocking or leaked critical section at 0x0e758a00 owned by thread 26 in w3wp.exe__hg__PID__3904__Date__03_07_2013__Time_11_01_07AM__974__Manual Dump.dmp 

Impact of this lock    
1.85% of threads blocked

(Threads 39)

The following functions are trying to enter this critical section

clr!UnsafeEEEnterCriticalSection+1c    

The following module(s) are involved with this critical section

C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll

======================================================================================

Thread 26 - System ID 7628
Entry point   0x00000000 
Create time   2013/3/7 10:44:15 
Time spent in user mode   0 Days 00:00:00.218 
Time spent in kernel mode   0 Days 00:00:00.343 




This thread is waiting to enter a .NET Lock which thread 39 is currently holding



.NET Call Stack



Function 
System.Threading.Monitor.ReliableEnter(System.Object, Boolean ByRef) 
System.Threading.Monitor.Enter(System.Object, Boolean ByRef) 
Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Container.ResolveComponent(Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext, Autofac.Core.Service, System.Collections.Generic.IEnumerable`1, System.Object ByRef) 
Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext, Autofac.Core.Service, System.Collections.Generic.IEnumerable`1) 
Autofac.ResolutionExtensions.Resolve[[System.__Canon, mscorlib]](Autofac.IComponentContext, System.Collections.Generic.IEnumerable`1) 
Autofac.ResolutionExtensions.Resolve[[System.__Canon, mscorlib]](Autofac.IComponentContext) 
Tunynet.DIContainer.Resolve[[System.__Canon, mscorlib]]() 
Spacebuilder.Common.ProfileRepository..cctor() 
Spacebuilder.Common.ProfileRepository..ctor() 
Spacebuilder.Common.UserProfileService..ctor() 
Spacebuilder.Common.User.get_Profile() 
ASP._Page_Themes_Channel_Default_Views_FindUser__QuickSearch_cshtml.Execute() 
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
Tunynet.UI.ThemedWebViewPage.ExecutePageHierarchy() 
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter, System.Web.WebPages.WebPageRenderingBase) 
Tunynet.UI.ThemedRazorView.RenderView(System.Web.Mvc.ViewContext, System.IO.TextWriter, System.Object) 
System.Web.Mvc.BuildManagerCompiledView.Render(System.Web.Mvc.ViewContext, System.IO.TextWriter) 
System.Web.Mvc.ViewResultBase.ExecuteResult(System.Web.Mvc.ControllerContext) 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionResult) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c.b__19() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter, System.Web.Mvc.ResultExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.b__1b() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter, System.Web.Mvc.ResultExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.b__1b() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter, System.Web.Mvc.ResultExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.b__1b() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter, System.Web.Mvc.ResultExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.b__1b() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(System.Web.Mvc.ControllerContext, System.Collections.Generic.IList`1, System.Web.Mvc.ActionResult) 
System.Web.Mvc.ControllerActionInvoker.InvokeAction(System.Web.Mvc.ControllerContext, System.String) 
System.Web.Mvc.Controller.ExecuteCore() 
System.Web.Mvc.ControllerBase.Execute(System.Web.Routing.RequestContext) 
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(System.Web.Routing.RequestContext) 
System.Web.Mvc.MvcHandler+<>c__DisplayClass6+<>c__DisplayClassb.b__5() 
System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass1.b__0() 
System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass8`1[[System.Web.Mvc.Async.AsyncVoid, System.Web.Mvc]].b__7(System.IAsyncResult) 
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResult`1[[System.Web.Mvc.Async.AsyncVoid, System.Web.Mvc]].End() 
System.Web.Mvc.MvcHandler+<>c__DisplayClasse.b__d() 
System.Web.Mvc.SecurityUtil.b__0(System.Action) 
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(System.Action) 
System.Web.Mvc.MvcHandler.EndProcessRequest(System.IAsyncResult) 
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(System.IAsyncResult) 
System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
System.Web.HttpApplication.ExecuteStep(IExecutionStep, Boolean ByRef) 
System.Web.HttpApplication+PipelineStepManager.ResumeSteps(System.Exception) 
System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext, System.AsyncCallback) 
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest, System.Web.HttpContext) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32) 
DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, System.Web.RequestNotificationStatus ByRef) 
System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr, System.Web.RequestNotificationStatus ByRef) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32) 


Full Call Stack



Function   Source 
ntdll!KiFastSystemCallRet    
ntdll!NtWaitForMultipleObjects+c    
KERNELBASE!WaitForMultipleObjectsEx+100    
kernel32!WaitForMultipleObjectsExImplementation+e0    
clr!WaitForMultipleObjectsEx_SO_TOLERANT+56    
clr!Thread::DoAppropriateAptStateWait+4d    
clr!Thread::DoAppropriateWaitWorker+17d    
clr!Thread::DoAppropriateWait+60    
clr!CLREvent::WaitEx+106    
clr!CLREvent::Wait+19    
clr!AwareLock::EnterEpilogHelper+a8    
clr!AwareLock::EnterEpilog+42    
clr!AwareLock::Enter+5f    
clr!AwareLock::Contention+228    
clr!JITutil_MonReliableContention+e8    
System.Threading.Monitor.Enter(System.Object, Boolean ByRef)    
clr!CallDescrWorker+33    
clr!CallDescrWorkerWithHandler+8e    
clr!DispatchCallBody+20    
clr!DispatchCallDebuggerWrapper+75    
clr!DispatchCallNoEH+53    
clr!MethodTable::RunClassInitEx+f1    
clr!MethodTable::DoRunClassInitThrowing+53e    
clr!MethodDesc::DoPrestub+f1    
clr!PreStubWorker+12c    
0x00f80842    
clr!MethodTable::GetRestoredSlot+2a    
clr!MethodDesc::GetMethodEntryPoint+4e    
clr!MethodDesc::DoPrestub+51d    
clr!PreStubWorker+15d    
0x00f80842    
System.Web.WebPages.WebPageBase.ExecutePageHierarchy()    
System.Web.Mvc.WebViewPage.ExecutePageHierarchy()    
System.Collections.Generic.Stack`1[[System.__Canon, mscorlib]].Push(System.__Canon)    
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter, System.Web.WebPages.WebPageRenderingBase)    
System.RuntimeType+ActivatorCacheEntry..ctor(System.Type, System.RuntimeMethodHandleInternal, Boolean)    
System.Web.Mvc.BuildManagerCompiledView.Render(System.Web.Mvc.ViewContext, System.IO.TextWriter)    
System.Web.Mvc.ViewResultBase.ExecuteResult(System.Web.Mvc.ControllerContext)    
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionResult)    
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()    
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(System.Web.Mvc.IResultFilter, System.Web.Mvc.ResultExecutingContext, System.Func`1<System.Web.Mvc.ResultExecutedContext>)    
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()    
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()    
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()    
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c+<>c__DisplayClass1e.<InvokeActionResultWithFilters>b__1b()    
System.Web.Mvc.ControllerActionInvoker.InvokeAction(System.Web.Mvc.ControllerContext, System.String)    
System.Web.Mvc.Controller.ExecuteCore()    
System.Web.Mvc.ControllerBase.Execute(System.Web.Routing.RequestContext)    
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(System.Web.Routing.RequestContext)    
System.Web.Mvc.MvcHandler+<>c__DisplayClass6+<>c__DisplayClassb.<BeginProcessRequest>b__5()    
System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass1.<MakeVoidDelegate>b__0()    
System.Web.Mvc.MvcHandler+<>c__DisplayClasse.<EndProcessRequest>b__d()    
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(System.Action)    
System.Web.Mvc.MvcHandler.EndProcessRequest(System.IAsyncResult)    
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(System.IAsyncResult)    
System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()    
System.Web.HttpApplication.ExecuteStep(IExecutionStep, Boolean ByRef)    
System.Web.HttpApplication+PipelineStepManager.ResumeSteps(System.Exception)    
System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext, System.AsyncCallback)    
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest, System.Web.HttpContext)    
System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)    
System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)    
webengine4!W3_MGD_HANDLER::ProcessNotification+5b    
webengine4!W3_MGD_HANDLER::DoWork+250    
webengine4!RequestDoWork+2da    
webengine4!CMgdEngHttpModule::OnExecuteRequestHandler+1a    
iiscore!NOTIFICATION_CONTEXT::RequestDoWork+128    
iiscore!NOTIFICATION_CONTEXT::CallModulesInternal+305    
iiscore!NOTIFICATION_CONTEXT::CallModules+28    
iiscore!W3_CONTEXT::DoStateRequestExecuteHandler+36    
iiscore!W3_CONTEXT::DoWork+d7    
iiscore!W3_MAIN_CONTEXT::ContinueNotificationLoop+1f    
iiscore!W3_MAIN_CONTEXT::ProcessIndicateCompletion+1f    
iiscore!W3_CONTEXT::IndicateCompletion+75    
webengine4!W3_MGD_HANDLER::IndicateCompletion+3d    
webengine4!MgdIndicateCompletion+24    
DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, System.Web.RequestNotificationStatus ByRef)    
System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)    
System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)    
clr!UM2MThunk_WrapperHelper+10    
clr!UM2MThunk_Wrapper+8c    
clr!Thread::DoADCallBack+f0    
clr!UM2MDoADCallBack+c0    
0x00f98c20    
webengine4!W3_MGD_HANDLER::ProcessNotification+5b    
webengine4!ProcessNotificationCallback+36    
clr!UnManagedPerAppDomainTPCount::DispatchWorkItem+195    
clr!ThreadpoolMgr::NewWorkerThreadStart+20b    
clr!ThreadpoolMgr::WorkerThreadStart+3d1    
clr!Thread::intermediateThreadProc+4b    
kernel32!BaseThreadInitThunk+e    
ntdll!__RtlUserThreadStart+70    
ntdll!_RtlUserThreadStart+1b 


=========================================================================

Thread 39 - System ID 4176
Entry point   0x00000000 
Create time   2013/3/7 10:44:21 
Time spent in user mode   0 Days 00:00:00.140 
Time spent in kernel mode   0 Days 00:00:00.109 




This thread is not fully resolved and may or may not be a problem. Further analysis of these threads may be required.



.NET Call Stack



Function 
Spacebuilder.Common.ProfileRepository..ctor() 
Spacebuilder.Common.UserProfileService..ctor() 
Spacebuilder.Common.UserSearcher..ctor(System.String, System.String, Boolean, Int32) 
Spacebuilder.Environments.Starter.b__54(Autofac.IComponentContext) 
Autofac.RegistrationExtensions+<>c__DisplayClass10`1[[System.__Canon, mscorlib]].b__f(Autofac.IComponentContext, System.Collections.Generic.IEnumerable`1) 
Autofac.Builder.RegistrationBuilder+<>c__DisplayClass1`1[[System.__Canon, mscorlib]].b__0(Autofac.IComponentContext, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Resolving.InstanceLookup.b__0() 
Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(System.Guid, System.Func`1) 
Autofac.Core.Resolving.InstanceLookup.Execute() 
Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope, Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Resolving.InstanceLookup.ResolveComponent(Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.Features.Collections.CollectionRegistrationSource+<>c__DisplayClass4+<>c__DisplayClass6.b__1(Autofac.Core.IComponentRegistration) 
System.Linq.Enumerable+WhereSelectArrayIterator`2[[System.__Canon, mscorlib],[System.__Canon, mscorlib]].MoveNext() 
System.Linq.Buffer`1[[System.__Canon, mscorlib]]..ctor(System.Collections.Generic.IEnumerable`1) 
System.Linq.Enumerable.ToArray[[System.__Canon, mscorlib]](System.Collections.Generic.IEnumerable`1) 
Autofac.Features.Collections.CollectionRegistrationSource+<>c__DisplayClass4.b__0(Autofac.IComponentContext, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(Autofac.IComponentContext, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Resolving.InstanceLookup.Activate(System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Resolving.InstanceLookup.Execute() 
Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(Autofac.Core.ISharingLifetimeScope, Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Resolving.ResolveOperation.Execute(Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.Core.Container.ResolveComponent(Autofac.Core.IComponentRegistration, System.Collections.Generic.IEnumerable`1) 
Autofac.ResolutionExtensions.TryResolveService(Autofac.IComponentContext, Autofac.Core.Service, System.Collections.Generic.IEnumerable`1, System.Object ByRef) 
Autofac.ResolutionExtensions.ResolveService(Autofac.IComponentContext, Autofac.Core.Service, System.Collections.Generic.IEnumerable`1) 
Autofac.ResolutionExtensions.Resolve[[System.__Canon, mscorlib]](Autofac.IComponentContext, System.Collections.Generic.IEnumerable`1) 
Autofac.ResolutionExtensions.Resolve[[System.__Canon, mscorlib]](Autofac.IComponentContext) 
Tunynet.DIContainer.Resolve[[System.__Canon, mscorlib]]() 
Spacebuilder.Search.SearcherFactory.GetSearcher(System.String) 
Spacebuilder.Common.FindUserController._InterestedWithAll(Int32, Int32, System.String) 
DynamicClass.lambda_method(System.Runtime.CompilerServices.Closure, System.Web.Mvc.ControllerBase, System.Object[]) 
System.Web.Mvc.ActionMethodDispatcher.Execute(System.Web.Mvc.ControllerBase, System.Object[]) 
System.Web.Mvc.ReflectedActionDescriptor.Execute(System.Web.Mvc.ControllerContext, System.Collections.Generic.IDictionary`2) 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionDescriptor, System.Collections.Generic.IDictionary`2) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass15.b__12() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(System.Web.Mvc.IActionFilter, System.Web.Mvc.ActionExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass15+<>c__DisplayClass17.b__14() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(System.Web.Mvc.IActionFilter, System.Web.Mvc.ActionExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass15+<>c__DisplayClass17.b__14() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(System.Web.Mvc.IActionFilter, System.Web.Mvc.ActionExecutingContext, System.Func`1) 
System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass15+<>c__DisplayClass17.b__14() 
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(System.Web.Mvc.ControllerContext, System.Collections.Generic.IList`1, System.Web.Mvc.ActionDescriptor, System.Collections.Generic.IDictionary`2) 
System.Web.Mvc.ControllerActionInvoker.InvokeAction(System.Web.Mvc.ControllerContext, System.String) 
System.Web.Mvc.Controller.ExecuteCore() 
System.Web.Mvc.ControllerBase.Execute(System.Web.Routing.RequestContext) 
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(System.Web.Routing.RequestContext) 
System.Web.Mvc.MvcHandler+<>c__DisplayClass6+<>c__DisplayClassb.b__5() 
System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass1.b__0() 
System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass8`1[[System.Web.Mvc.Async.AsyncVoid, System.Web.Mvc]].b__7(System.IAsyncResult) 
System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResult`1[[System.Web.Mvc.Async.AsyncVoid, System.Web.Mvc]].End() 
System.Web.Mvc.MvcHandler+<>c__DisplayClasse.b__d() 
System.Web.Mvc.SecurityUtil.b__0(System.Action) 
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(System.Action) 
System.Web.Mvc.MvcHandler.EndProcessRequest(System.IAsyncResult) 
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(System.IAsyncResult) 
System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
System.Web.HttpApplication.ExecuteStep(IExecutionStep, Boolean ByRef) 
System.Web.HttpApplication+PipelineStepManager.ResumeSteps(System.Exception) 
System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext, System.AsyncCallback) 
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest, System.Web.HttpContext) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32) 
DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, System.Web.RequestNotificationStatus ByRef) 
System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr, System.Web.RequestNotificationStatus ByRef) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32) 
System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32) 


Full Call Stack



Function   Source 
ntdll!KiFastSystemCallRet    
ntdll!NtWaitForSingleObject+c    
ntdll!RtlpWaitOnCriticalSection+13e    
ntdll!RtlEnterCriticalSection+150    
clr!UnsafeEEEnterCriticalSection+1c    
clr!CrstBase::Enter+1ad    
clr!ListLockEntry::FinishDeadlockAwareEnter+25    
clr!ListLockEntry::LockHolder::DeadlockAwareAcquire+28    
clr!MethodTable::DoRunClassInitThrowing+4c2    
clr!MethodDesc::DoPrestub+f1    
clr!PreStubWorker+12c    
0x00f80842    
clr!PreStubWorker+165    
System.Linq.Enumerable+WhereSelectArrayIterator`2[[System.__Canon, mscorlib],[System.__Canon, mscorlib]].MoveNext()    
System.Linq.Buffer`1[[System.__Canon, mscorlib]]..ctor(System.Collections.Generic.IEnumerable`1<System.__Canon>)    
System.Linq.Enumerable.ToArray[[System.__Canon, mscorlib]](System.Collections.Generic.IEnumerable`1<System.__Canon>)    
System.Collections.Generic.Stack`1[[System.__Canon, mscorlib]].Push(System.__Canon)    
clr!JIT_Stelem_Ref+25    
System.Collections.Generic.Stack`1[[System.__Canon, mscorlib]].Push(System.__Canon) 

Alex 回复:

这听起来确实很棘手。 DIContainer 类在做什么 你有注册为 InstancePerHttpRequest 的组件吗? 或 InstancePerLifetimeScope?似乎也有一些石英工作 在后台,它们是如何与容器交互的?

特拉维斯回复:

快速查看堆栈跟踪表明您可能有一些 长时间运行的操作或发生在 Spacebuilder.Environments.Starter, Spacebuilder.Common.UserSearcher.ctor, Spacebuilder.UserProfileService.ctor,或 Spacebuilder.Common.ProfileRepository。我看到的是 有趣的是,在 RESOLVE 操作期间,无论何时 组件得到解决,有一个内部寄存器正在进行。 没有看到代码,我无法告诉你那里发生了什么,但是 那个线程 - #39 - 这就是我要看的东西。

看起来...正在调用一个控制器操作 有一些 一种在闭包上运行的 lambda (DynamicClass.lambda_method) - 在你的管道中定制一些东西。 SearcherFactory.GetSearcher 手动尝试解决 smothing。在决议期间 GetSearcher,有东西要求收集 - 比如 IEnumerable 服务。构造函数参数?这是它的地方 变得奇怪:在解决 IEnumerable 时,一个新的生命周期范围 正在创建,并且似乎正在注册某些内容 它? LifetimeScope.ResolveComponent 调用确实做了一个锁 确保分辨率是线程安全的。你陷入僵局的事实 意味着两个不同的线程正在尝试解决相同的问题 生命周期范围(容器?),在 MVC 中是 要避免的事情 - 例如,您可能不会遇到这种情况, 如果您只在 HttpRequest 生命周期范围之外解析,因为 您一次只能在一个线程上使用它。或者,您可以 为了执行目的,在应用程序中传递生命周期范围 服务位置,这也可能是一个危险信号。

有一个关于并发的 wiki 页面也可以给你一些 指点:https://code.google.com/p/autofac/wiki/Concurrency

如果这不能让你朝着你需要的方向前进,我建议你 再次发布到讨论论坛,因为我真的不能 必须及时提供个人帮助。那会得到 其他人的眼睛,可能会更快地得到你的帮助。您需要包括: 问题的描述(就像在这封电子邮件中一样),但有更多 有关您期望发生的事情的详细信息(例如,您正在访问一个 执行 blah blah blah 的控制器动作)。的堆栈跟踪 阻塞线程。事情被阻塞的代码: 您最初的 ContainerBuilder 注册以显示情况如何 注册。那些构造函数和东西里面有什么 挂了。该信息将帮助人们更快地根除 问题。这也是你应该尊重的东西 事情被锁定

我的补充问题详情:

服务器经常不规律的挂起,浏览器会突然停止响应我们使用iisreset命令重启服务器。我不确定当时是否调用了控制器或触发了某些 Quartz 任务。

在 Application_Start() 中,我们使用 ContainerBuilder.Register() 在同一根生命周期范围内注册许多组件。并且这些组件可以相互调用。

大部分注册为SingleInstance,如:

containerBuilder.Register(c => new DefaultCacheService(new MemcachedCache())).As<ICacheService>().SingleInstance();
containerBuilder.Register(c => new QuartzTaskScheduler()).As<ITaskScheduler>().SingleInstance();
containerBuilder.Register(c => new UserService()).As<IUserService>().SingleInstance();
containerBuilder.Register(c => new UserSearcher("~/App_Data/IndexFiles/User")).As<ISearcher>().SingleInstance();
containerBuilder.Register(c => new TagSearcher("~/App_Data/IndexFiles/Tag")).As<ISearcher>().SingleInstance();

只有少数注册为InstancePerHttpRequest,这些组件的初始化与每个请求相关。如:

containerBuilder.Register(c => new FormsAuthenticationService()).As<IAuthenticationService>().InstancePerHttpRequest();

我们提供了一个类(DIContainer),包括一些简单的静态方法来解析组件。在解析组件时调用静态方法,例如在某些 Controller、Service、Repository(DAO) 或 Quartz 任务中。

IContainer container = containerBuilder.Build();
DIContainer.RegisterContainer(container); 
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

public class DIContainer
{
   private static IContainer _container;

   public static void RegisterContainer(IContainer container)
   {
       _container = container;
   }

   public static TService Resolve<TService>()
   {
       return _container.Resolve<TService>();
   }
   ......
}

我们使用Lucene.Net做全文搜索服务,每个ISearcher对应一种搜索类型和一个索引目录。需要时在 SearcherFactory.GetSearcher() 中解析 ISearcher。

public static ISearcher GetSearcher(string IndexPath)
{
   return DIContainer.Resolve<IEnumerable<ISearcher>>().Where(s => s.IndexPath.Equals(IndexPath, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
}

是这样,但我仍然无法找出根本原因。希望得到您的帮助。

【问题讨论】:

  • 很难说没有看到所有代码和堆栈跟踪,但第一个危险信号是“我们提供了一个类(DIContainer),其中包含一些用于组件解析的简单静态方法。” static 代码。你为什么还要像这样继续解析ISearcher 实例?它们不是 SearcherFactory 生命周期的固定集合,因此被注入到它的构造函数中吗?
  • 鉴于持有锁的线程位于Spacebuilder.Common.ProfileRepository..ctor()Spacebuilder.Common.UserProfileService..ctor()Spacebuilder.Common.UserSearcher..ctor(System.String, System.String, Boolean, Int32)Spacebuilder.Environments.Starter.b__54(Autofac.IComponentContext) 链中,我们可能需要查看该代码(如我原始回复中所述)。

标签: iis deadlock autofac


【解决方案1】:

Resolve方法的内部实现是LifetimeScope.GetOrCreateAndShare,该方法使用线程锁。查看ProfileRepository的静态构造函数,可能构造函数的线程锁和autofac的线程锁构成死锁。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 2018-07-28
    相关资源
    最近更新 更多