aspnetboilerplate: App hangs - OnlineClientManager and UserSettingsManager
We have really been fighting a bad lock up issue with our application for a couple months. About every week our site is going down for roughly 30 minutes. After going through several support tickets and several memory dumps later, we have isolated the problem of the latest hang.
This issue will be opened for discussion of the particular problem we found with OnlineClientManger and UserSettingsManager. Overall, I think there needs to be a larger discussion of the overuse of AsyncHelper.RunSync and in general flipping between async to sync and sync to async. I will open another issue and will try and link that here once I have that issue opened. Starting here because I will begin looking at what it’s going to take to fix the issues identified below and wanted to have a place to document what I find and be able to ask questions. This has been a very serious issue for our application and needs to be addressed asap.
So, below is the response from microsoft support on what is going wrong. This is an analysis of three memory dumps that we took during the most recent outage. This will be a very long response I got from them but I felt it important to include everything so we can have a larger discussion about how to solve it.
FYI, we are using 4.0.1 and the code snippets below are from that version.
-----------FROM MICROSOFT SUPPORT---------------
We put the report together below, it’s the usage of mixing sync and async that’s causing the site problem, it’s more difficult to spot them.
Pattern A:
We observe 66 threads are waiting for the SyncObj lock to be acquired in Abp!Abp.RealTime.OnlineClientManager.Add(). These threads will be blocked until the lock on SyncObj is released. This SyncObj is held and acquired by thread 268. Unless thread 268 releases the lock, all the below threads will be blocked. So, to resolve the blocked threads we need to pay attention to what thread 268 is doing.
public virtual void Add(IOnlineClient client)
{
lock (SyncObj)
{
var userWasAlreadyOnline = false;
var user = client.ToUserIdentifierOrNull();
if (user != null)
{
userWasAlreadyOnline = this.IsOnline(user);
}
Clients[client.ConnectionId] = client;
ClientConnected.InvokeSafely(this, new OnlineClientEventArgs(client));
if (user != null && !userWasAlreadyOnline)
{
UserConnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
}
}
}
Callstack for Pattern A:
Pattern #A
66 threads [stats]: 29 34 41 50 59 66 68 73 81 85 ...
77e0f29c ntdll!NtWaitForMultipleObjects+0xc
77ca2420 KERNELBASE!WaitForMultipleObjectsEx+0xf0
73e497ae clr!WaitForMultipleObjectsEx_SO_TOLERANT+0x3c
(Inline) clr!Thread::DoAppropriateAptStateWait+0x35
73e494f5 clr!Thread::DoAppropriateWaitWorker+0x237
73e495e6 clr!Thread::DoAppropriateWait+0x64
73e49754 clr!CLREventBase::WaitEx+0x128
73d3ef1b clr!CLREventBase::Wait+0x1a
73da7f76 clr!AwareLock::EnterEpilogHelper+0xa8
73da7eb4 clr!AwareLock::EnterEpilog+0x48
73da8000 clr!AwareLock::Enter+0x4a
73d3171c clr!AwareLock::Contention+0x297
73da84e7 clr!JITutil_MonReliableContention+0x8a
2d14f55e **Abp!Abp.RealTime.OnlineClientManager.Add(Abp.RealTime.IOnlineClient)+0x3e**
2d14f1cf Abp_Web_SignalR!Abp.Web.SignalR.Hubs.AbpCommonHub+<OnReconnected>d__5.MoveNext()+0xc7
2d14f0c3 mscorlib_ni!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[System.__Canon, mscorlib]](System.__Canon ByRef)+0xffffffffb9c74813
2d14f05a Abp_Web_SignalR!Abp.Web.SignalR.Hubs.AbpCommonHub.OnReconnected()+0x6a
2d14ebc9 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Reconnect(Microsoft.AspNet.SignalR.Hubs.IHub)+0x9
2d14eb29 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubPipelineModule+<>c__DisplayClass2_0.<BuildReconnect>b__0(Microsoft.AspNet.SignalR.Hubs.IHub)+0x29
2d14eaed Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubPipeline.Reconnect(Microsoft.AspNet.SignalR.Hubs.IHub)+0x1d
2d14eabc Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.<OnReconnected>b__31_0(Microsoft.AspNet.SignalR.Hubs.IHub)+0xc
2d14ea72 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher+<>c__DisplayClass35_0.<ExecuteHubEvent>b__0(Microsoft.AspNet.SignalR.Hubs.IHub)+0x12
20e2b10a System_Core!System.Linq.Enumerable+WhereSelectListIterator`2[[System.Web.DynamicModuleRegistryEntry, System.Web],[System.__Canon, mscorlib]].MoveNext()+0x149e8a
1f3a7022 System_Core!System.Linq.Buffer`1[[System.__Canon, mscorlib]]..ctor(System.Collections.Generic.IEnumerable`1<System.__Canon>)+0xda
2260c88d System_Core!System.Linq.Enumerable.ToArray[[System.Boolean, mscorlib]](System.Collections.Generic.IEnumerable`1<Boolean>)+0xfffffffffde131dd
2d14df5a Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ExecuteHubEvent(Microsoft.AspNet.SignalR.IRequest, System.String, System.Func`2<Microsoft.AspNet.SignalR.Hubs.IHub,System.Threading.Tasks.Task>)+0x82
2d14debf Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReconnected(Microsoft.AspNet.SignalR.IRequest, System.String)+0x3f
2d14de5a Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection+<>c__DisplayClass64_0.<ProcessRequestPostGroupRead>b__3()+0x1a
2d14de0d Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(System.Func`1<System.Threading.Tasks.Task>)+0x1d
2d14ddd9 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection+<>c__DisplayClass64_0.<ProcessRequestPostGroupRead>b__2()+0x39
2c4d80b1 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport+<>c__DisplayClass41_0.<ProcessReceiveRequest>b__4()+0x11
2c4d637f Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessMessages(Microsoft.AspNet.SignalR.Transports.ITransportConnection, System.Func`1<System.Threading.Tasks.Task>)+0x22f
2c4d5d7e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessReceiveRequest(Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0x206
2c4d5b5e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport+<>c.<ProcessRequestCore>b__34_0(Microsoft.AspNet.SignalR.Transports.ForeverTransport, Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0xe
2c4d5b18 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Func`3<System.__Canon,System.__Canon,System.Threading.Tasks.Task>, System.__Canon, System.__Canon)+0x20
2c4d5a8f Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.Then[[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Threading.Tasks.Task, System.Func`3<System.__Canon,System.__Canon,System.Threading.Tasks.Task>, System.__Canon, System.__Canon)+0x5f
2c4d4d8e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessRequestCore(Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0xce
2c4d4ca6 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessRequest(Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0x6
2c4d3737 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequestPostGroupRead(Microsoft.AspNet.SignalR.Hosting.HostContext, System.String)+0x197
2c4d3589 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection+<>c.<ProcessRequest>b__63_0(System.String, Microsoft.AspNet.SignalR.PersistentConnection, Microsoft.AspNet.SignalR.Hosting.HostContext)+0x11
2c4d350b Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Func`4<System.__Canon,System.__Canon,System.__Canon,System.__Canon>, System.__Canon, System.__Canon, System.__Canon)+0x33
2c4d3477 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.Then[[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Threading.Tasks.Task`1<System.__Canon>, System.Func`4<System.__Canon,System.__Canon,System.__Canon,System.__Canon>, System.__Canon, System.__Canon)+0xc7
28f2365e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(Microsoft.AspNet.SignalR.Hosting.HostContext)+0x20e
28f2325e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ProcessRequest(Microsoft.AspNet.SignalR.Hosting.HostContext)+0xee
28f21c4c Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(System.Collections.Generic.IDictionary`2<System.String,System.Object>)+0x84
28ddd62e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Owin.Middleware.HubDispatcherMiddleware.Invoke(Microsoft.Owin.IOwinContext)+0x6e
248bff36 Microsoft_Owin!Microsoft.Owin.Infrastructure.OwinMiddlewareTransition.Invoke(System.Collections.Generic.IDictionary`2<System.String,System.Object>)+0x2e
28ddd379 Microsoft_Owin!Microsoft.Owin.Mapping.MapMiddleware+<Invoke>d__0.MoveNext()+0x111
…[Snip]
749d62c4 kernel32!BaseThreadInitThunk+0x24
77e01f69 ntdll!__RtlUserThreadStart+0x2f
77e01f34 ntdll!_RtlUserThreadStart+0x1b
Pattern B
In this pattern, we observe 145 threads waiting for the SyncObj lock to be acquired in Abp!Abp.RealTime.OnlineClientManager.GetByConnectionIdOrNull(). All these 145 threads will be blocked until the lock on SyncObj is released. Notice that this is the exact same SyncObj which Pattern #A is waiting on. This SyncObj is held and acquired by thread 268. Unless thread 268 releases the lock, all the below threads will be blocked. So, to resolve the blocked threads we need to pay attention to what thread 268 is doing.
public virtual IOnlineClient GetByConnectionIdOrNull(string connectionId)
{
lock (SyncObj)
{
return Clients.GetOrDefault(connectionId);
}
}
Callstack for Pattern B:
145 threads [stats]: 33 39 48 57 67 75 77 79 83 87 ...
77e0f29c ntdll!NtWaitForMultipleObjects+0xc
77ca2420 KERNELBASE!WaitForMultipleObjectsEx+0xf0
73e497ae clr!WaitForMultipleObjectsEx_SO_TOLERANT+0x3c
(Inline) clr!Thread::DoAppropriateAptStateWait+0x35
73e494f5 clr!Thread::DoAppropriateWaitWorker+0x237
73e495e6 clr!Thread::DoAppropriateWait+0x64
73e49754 clr!CLREventBase::WaitEx+0x128 (f:\dd\ndp\clr\src\vm\synch.cpp @ 753)
73d3ef1b clr!CLREventBase::Wait+0x1a (f:\dd\ndp\clr\src\vm\synch.cpp @ 674)
73da7f76 clr!AwareLock::EnterEpilogHelper+0xa8 (f:\dd\ndp\clr\src\vm\syncblk.cpp @ 3119)
73da7eb4 clr!AwareLock::EnterEpilog+0x48 (f:\dd\ndp\clr\src\vm\syncblk.cpp @ 3064)
73da8000 clr!AwareLock::Enter+0x4a (f:\dd\ndp\clr\src\vm\syncblk.cpp @ 2900)
73d3171c clr!AwareLock::Contention+0x297 (f:\dd\ndp\clr\src\vm\syncblk.cpp @ 3411)
73da84e7 clr!JITutil_MonReliableContention+0x8a (f:\dd\ndp\clr\src\vm\jithelpers.cpp @ 4988)
2d14f2bd Abp!Abp.RealTime.OnlineClientManager.GetByConnectionIdOrNull(System.String)+0x3d
2d14f1b4 Abp_Web_SignalR!Abp.Web.SignalR.Hubs.AbpCommonHub+<OnReconnected>d__5.MoveNext()+0xac
2d14f0c3 mscorlib_ni!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[System.__Canon, mscorlib]](System.__Canon ByRef)+0xffffffffb9c74813 (f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs @ 304)
2d14f05a Abp_Web_SignalR!Abp.Web.SignalR.Hubs.AbpCommonHub.OnReconnected()+0x6a
2d14ebc9 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.Reconnect(Microsoft.AspNet.SignalR.Hubs.IHub)+0x9
2d14eb29 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubPipelineModule+<>c__DisplayClass2_0.<BuildReconnect>b__0(Microsoft.AspNet.SignalR.Hubs.IHub)+0x29
2d14eaed Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubPipeline.Reconnect(Microsoft.AspNet.SignalR.Hubs.IHub)+0x1d
2d14eabc Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.<OnReconnected>b__31_0(Microsoft.AspNet.SignalR.Hubs.IHub)+0xc
2d14ea72 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher+<>c__DisplayClass35_0.<ExecuteHubEvent>b__0(Microsoft.AspNet.SignalR.Hubs.IHub)+0x12
20e2b10a System_Core!System.Linq.Enumerable+WhereSelectListIterator`2[[System.Web.DynamicModuleRegistryEntry, System.Web],[System.__Canon, mscorlib]].MoveNext()+0x149e8a (f:\dd\ndp\fx\src\Core\System\Linq\Enumerable.cs @ 493)
1f3a7022 System_Core!System.Linq.Buffer`1[[System.__Canon, mscorlib]]..ctor(System.Collections.Generic.IEnumerable`1<System.__Canon>)+0xda (f:\dd\ndp\fx\src\Core\System\Linq\Enumerable.cs @ 2681)
2260c88d System_Core!System.Linq.Enumerable.ToArray[[System.Boolean, mscorlib]](System.Collections.Generic.IEnumerable`1<Boolean>)+0xfffffffffde131dd (f:\dd\ndp\fx\src\Core\System\Linq\Enumerable.cs @ 943)
2d14df5a Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ExecuteHubEvent(Microsoft.AspNet.SignalR.IRequest, System.String, System.Func`2<Microsoft.AspNet.SignalR.Hubs.IHub,System.Threading.Tasks.Task>)+0x82
2d14debf Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.OnReconnected(Microsoft.AspNet.SignalR.IRequest, System.String)+0x3f
2d14de5a Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection+<>c__DisplayClass64_0.<ProcessRequestPostGroupRead>b__3()+0x1a
2d14de0d Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod(System.Func`1<System.Threading.Tasks.Task>)+0x1d
2d14ddd9 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection+<>c__DisplayClass64_0.<ProcessRequestPostGroupRead>b__2()+0x39
2c4d80b1 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport+<>c__DisplayClass41_0.<ProcessReceiveRequest>b__4()+0x11
2c4d637f Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessMessages(Microsoft.AspNet.SignalR.Transports.ITransportConnection, System.Func`1<System.Threading.Tasks.Task>)+0x22f
2c4d5d7e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessReceiveRequest(Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0x206
2c4d5b5e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport+<>c.<ProcessRequestCore>b__34_0(Microsoft.AspNet.SignalR.Transports.ForeverTransport, Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0xe
2c4d5b18 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Func`3<System.__Canon,System.__Canon,System.Threading.Tasks.Task>, System.__Canon, System.__Canon)+0x20
2c4d5a8f Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.Then[[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Threading.Tasks.Task, System.Func`3<System.__Canon,System.__Canon,System.Threading.Tasks.Task>, System.__Canon, System.__Canon)+0x5f
2c4d4d8e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessRequestCore(Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0xce
2c4d4ca6 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Transports.ForeverTransport.ProcessRequest(Microsoft.AspNet.SignalR.Transports.ITransportConnection)+0x6
2c4d3737 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequestPostGroupRead(Microsoft.AspNet.SignalR.Hosting.HostContext, System.String)+0x197
2c4d3589 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection+<>c.<ProcessRequest>b__63_0(System.String, Microsoft.AspNet.SignalR.PersistentConnection, Microsoft.AspNet.SignalR.Hosting.HostContext)+0x11
2c4d350b Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.FromMethod[[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Func`4<System.__Canon,System.__Canon,System.__Canon,System.__Canon>, System.__Canon, System.__Canon, System.__Canon)+0x33
2c4d3477 Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.TaskAsyncHelper.Then[[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib],[System.__Canon, mscorlib]](System.Threading.Tasks.Task`1<System.__Canon>, System.Func`4<System.__Canon,System.__Canon,System.__Canon,System.__Canon>, System.__Canon, System.__Canon)+0xc7
28f2365e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(Microsoft.AspNet.SignalR.Hosting.HostContext)+0x20e
28f2325e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Hubs.HubDispatcher.ProcessRequest(Microsoft.AspNet.SignalR.Hosting.HostContext)+0xee
28f21c4c Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.PersistentConnection.ProcessRequest(System.Collections.Generic.IDictionary`2<System.String,System.Object>)+0x84
28ddd62e Microsoft_AspNet_SignalR_Core!Microsoft.AspNet.SignalR.Owin.Middleware.HubDispatcherMiddleware.Invoke(Microsoft.Owin.IOwinContext)+0x6e
248bff36 Microsoft_Owin!Microsoft.Owin.Infrastructure.OwinMiddlewareTransition.Invoke(System.Collections.Generic.IDictionary`2<System.String,System.Object>)+0x2e
28ddd379 Microsoft_Owin!Microsoft.Owin.Mapping.MapMiddleware+<Invoke>d__0.MoveNext()+0x111
28ddd223 mscorlib_ni!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[System.__Canon, mscorlib]](System.__Canon ByRef)+0xffffffffb5902973 (f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs @ 304)
28ddd1bd Microsoft_Owin!Microsoft.Owin.Mapping.MapMiddleware.Invoke(System.Collections.Generic.IDictionary`2<System.String,System.Object>)+0x6d
2792ea0d Microsoft_Owin_Host_SystemWeb!Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage+<RunApp>d__5.MoveNext()+0x35 (c:\BuildAgent\work\edf281db05823b75\RuntimeRelease\src\Microsoft.Owin.Host.SystemWeb\IntegratedPipeline\IntegratedPipelineContextStage.cs @ 83)
2792e993 mscorlib_ni!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[[System.__Canon, mscorlib]](System.__Canon ByRef)+0xffffffffb44540e3 (f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs @ 304)
2792e92f Microsoft_Owin_Host_SystemWeb!Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.RunApp(System.Func`2<System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Threading.Tasks.Task>, System.Collections.Generic.IDictionary`2<System.String,System.Object>, System.Threading.Tasks.TaskCompletionSource`1<System.Object>, Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult)+0x7f
2792caef Microsoft_Owin_Host_SystemWeb!Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.BeginEvent(System.Object, System.EventArgs, System.AsyncCallback, System.Object)+0x1a7
…[Snip]
77e01f69 ntdll!__RtlUserThreadStart+0x2f (d:\rs1\minkernel\ntdll\rtlstrt.c @ 997)
77e01f34 ntdll!_RtlUserThreadStart+0x1b (d:\rs1\minkernel\ntdll\rtlstrt.c @ 914)
Now, reviewing the callstack for thread 268 we observe that the SyncObj lock is held and acquired inside the function Abp!Abp.RealTime.OnlineClientManager.Add(Abp.RealTime.IOnlineClient)+0x130. Next, we observe the thread is calling UserConnected.Invokesafely() function synchronously.

Eventually InvokeSafely() method, calls Friendships.Cache.UserFriendsCache.GetUserFriendsCacheItemInternal() method which further calls the ASP.NET Identity’s synchronous method “UserManagerExtensions.FindById()”. Now the implementation of the synchronous FindById() is actually a call to AsyncHelper.RunSync method. So the FindById() method is a sync wrapper over the async function FindbyIdAsync().

Now calling sync over async is discouraged and should be avoided, or else it will lead to various problems including hangs. For example, see https://stackoverflow.com/questions/45794017/asp-net-mvc-identity-framework-hangs-on-findbyemail-call-in-development-but-not Here we observe the case of “sync over async” i.e. having a sync wrapper (FindById) over an async implementation (FindByIdAsync). As documented https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods, the best practice is to “Avoid Exposing Synchronous Wrappers for Asynchronous Implementations”. It is strongly suggested that if you have an API which internally is implemented synchronously, you should not expose an asynchronous counterpart that simply wraps the synchronous method in Task.Run. Rather, if a consumer of your library wants to use the synchronous method asynchronously, they can do so on their own. … Similar guidance applies in the reverse direction as well: if you expose an asynchronous endpoint from your library, avoid exposing a synchronous method that just wraps the asynchronous implementation. Doing so hides from the consumer the true nature of the implementation, and it should be left up to the consumer how they want to consume the implementation. If the consumer chooses to block waiting for the asynchronous implementation to complete, that’s up to the caller, and they can do so with their eyes wide open. This is even more important for the “sync over async” case than it is for the “async over sync” case, because for “sync over async,” it can lead to significant problems with the application, such as hangs. To resolve this issue, we suggest not mixing sync over async and going async all the way down and making code changes to replace synchronous FindById() with async call to FindByIdAsync().
Pattern C
In this pattern, we observe 405 threads blocked on an synchronous function Abp!Abp.Configuration.SettingManagerExtensions.GetSettingValueForUser () which internally calls an async implementation settingManager.GetSettingValueForUserAsync(name, tenantId, userId, fallbackToDefault) which gets executed in a synchronously by using AsyncHelper.RunSync().
public static string GetSettingValueForUser(this ISettingManager settingManager, string name, int? tenantId, long userId, bool fallbackToDefault)
{
return AsyncHelper.RunSync(() => settingManager.GetSettingValueForUserAsync(name, tenantId, userId, fallbackToDefault));
}
Here we observe the case of “sync over async” i.e. having a sync wrapper (Abp!Abp.Configuration.SettingManagerExtensions.GetSettingValueForUser()) over an async implementation (settingManager.GetSettingValueForUserAsync(name, tenantId, userId, fallbackToDefault)). As documented https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/ , the best practice is to “Avoid Exposing Synchronous Wrappers for Asynchronous Implementations”. It is strongly suggested that if you have an API which internally is implemented synchronously, you should not expose an asynchronous counterpart that simply wraps the synchronous method in Task.Run. Rather, if a consumer of your library wants to use the synchronous method asynchronously, they can do so on their own. … Similar guidance applies in the reverse direction as well: if you expose an asynchronous endpoint from your library, for example settingManager.GetSettingValueForUserAsync(), avoid exposing a synchronous method that just wraps the asynchronous implementation i.e. Abp!Abp.Configuration.SettingManagerExtensions.GetSettingValueForUser(). Doing so hides from the consumer the true nature of the implementation, and it should be left up to the consumer how they want to consume the implementation. If the consumer chooses to block waiting for the asynchronous implementation to complete, that’s up to the caller, and they can do so with their eyes wide open. This is even more important for the “sync over async” case than it is for the “async over sync” case, because for “sync over async,” it can lead to significant problems with the application, such as hangs.
Also, the Nito library https://github.com/StephenCleary/AsyncEx/blob/master/doc/AsyncContext.md is best to be suited for applications like console and Win32 services which needs a single threaded context where ASP.NET context is not available, however for ASP.NET MVC apps this library usage to call “sync over async” should really be careful and last option and should be avoided…
For more information, see https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/
Callstack Pattern C
77e0f29c ntdll!NtWaitForMultipleObjects+0xc
77ca2420 KERNELBASE!WaitForMultipleObjectsEx+0xf0
73e497ae clr!WaitForMultipleObjectsEx_SO_TOLERANT+0x3c
(Inline) clr!Thread::DoAppropriateAptStateWait+0x35
73e494f5 clr!Thread::DoAppropriateWaitWorker+0x237
73e495e6 clr!Thread::DoAppropriateWait+0x64
73e49754 clr!CLREventBase::WaitEx+0x128
73d3ef1b clr!CLREventBase::Wait+0x1a
(Inline) clr!Thread::Wait+0x17
73d38b80 clr!Thread::Block+0x25
73d38cba clr!SyncBlock::Wait+0x189
(Inline) clr!ObjHeader::Wait+0x24
(Inline) clr!Object::Wait+0x24
73d38d9a clr!ObjectNative::WaitTimeout+0xcb
72bd5fe7 mscorlib_ni!System.Threading.Monitor.Wait(System.Object, Int32, Boolean)+0x17
72be7bd7 mscorlib_ni!System.Threading.SemaphoreSlim.WaitUntilCountOrTimeout(Int32, UInt32, System.Threading.CancellationToken)+0x4b
72be7b20 mscorlib_ni!System.Threading.SemaphoreSlim.Wait(Int32, System.Threading.CancellationToken)+0x1f0
1f0989c3 System!System.Collections.Concurrent.BlockingCollection`1[[System.__Canon, mscorlib]].TryTakeWithNoTimeValidation(System.__Canon ByRef, Int32, System.Threading.CancellationToken, System.Threading.CancellationTokenSource)+0xeb
1f09880a System!System.Collections.Concurrent.BlockingCollection`1+<GetConsumingEnumerable>d__68[[System.__Canon, mscorlib]].MoveNext()+0xa2
1f08605c Nito_AsyncEx_Context!Nito.AsyncEx.AsyncContext.Execute()+0x94
1f085bf6 Nito_AsyncEx_Context!Nito.AsyncEx.AsyncContext.Run[[System.Boolean, mscorlib]](System.Func`1<System.Threading.Tasks.Task`1<Boolean>>)+0xfffffffff65d23e6
1f0856ae Abp!Abp.Configuration.SettingManagerExtensions.GetSettingValueForUser(Abp.Configuration.ISettingManager, System.String, System.Nullable`1<Int32>, Int64, Boolean)+0x6e
1f084dcf Abp_Web!Abp.Web.Localization.CurrentCultureSetter.GetCultureFromUserSetting()+0x87
1f084b6f Abp_Web!Abp.Web.Localization.CurrentCultureSetter.SetCurrentCulture(System.Web.HttpContext)+0x57
1f084b01 Abp_Web!Abp.Web.AbpWebApplication`1[[System.__Canon, mscorlib]].<SetCurrentCulture>b__12_0(Abp.Web.Localization.ICurrentCultureSetter)+0x19
1f084a42 Abp!Abp.Dependency.IocResolverExtensions.Using[[System.__Canon, mscorlib]](Abp.Dependency.IIocResolver, System.Action`1<System.__Canon>)+0x72
1f0849b5 Abp_Web!Abp.Web.AbpWebApplication`1[[System.__Canon, mscorlib]].SetCurrentCulture()+0x55
1f08494b Abp_Web!Abp.Web.AbpWebApplication`1[[System.__Canon, mscorlib]].Application_PostAuthenticateRequest(System.Object, System.EventArgs)+0xb
276ac57a System_Web!System.Web.HttpApplication+SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+0xca
276ac495 System_Web!System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep)+0x85
276ac18a System_Web!System.Web.HttpApplication.ExecuteStep(IExecutionStep, Boolean ByRef)+0x4a
276a87cb System_Web!System.Web.HttpApplication+PipelineStepManager.ResumeSteps(System.Exception)+0x5c3
276a7de5 System_Web!System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext, System.AsyncCallback)+0x65
276a2d59 System_Web!System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest, System.Web.HttpContext)+0x1b9
276a1301 System_Web!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)+0x3a9
276a0f28 System_Web!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)+0x20
061be736 CLRStub[StubLinkStub]@d118d4cf061be736
6ef1ab23 webengine4!W3_MGD_HANDLER::ProcessNotification+0x62
6ef1bba5 webengine4!W3_MGD_HANDLER::DoWork+0x32a
6ef1aac2 webengine4!RequestDoWork+0x39f
6ef7acf5 webengine4!CMgdEngHttpModule::OnPostAuthenticateRequest+0x15
6f2b809b iiscore!NOTIFICATION_CONTEXT::RequestDoWork+0x2e3
6f2b72e3 iiscore!NOTIFICATION_CONTEXT::CallModulesInternal+
6f2a5e5b iiscore!NOTIFICATION_CONTEXT::CallModules+0x2b
6f2b8b05 iiscore!NOTIFICATION_MAIN::DoStateRequestAuthenticate+0x6a
6f2b8669 iiscore!NOTIFICATION_MAIN::DoWork+
6f2aa720 iiscore!W3_CONTEXT_BASE::ContinueNotificationLoop+0x32
(Inline) iiscore!W3_CONTEXT_BASE::ProcessIndicateCompletion+
6f2a9710 iiscore!W3_CONTEXT_BASE::IndicateCompletion+
6ef75afd webengine4!W3_MGD_HANDLER::IndicateCompletion+0x45
6ef77ea2 webengine4!MgdIndicateCompletion+0x22
276ae3c8 System_Web!DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, System.Web.RequestNotificationStatus ByRef)+0x60
276a1449 System_Web!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)+0x4f1
276a0f28 System_Web!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)+0x20
061be736 CLRStub[StubLinkStub]@d118d4cf061be736
73ddc9a1 clr!UM2MThunk_Wrapper+0x76
73ddc8d2 clr!Thread::DoADCallBack+
73ddc83a clr!UM2MDoADCallBack+
061be78f CLRStub[StubLinkStub]@d118d4cf061be78f
6ef1ab23 webengine4!W3_MGD_HANDLER::ProcessNotification+0x62
6ef1ab83 webengine4!ProcessNotificationCallback+0x33
6f54af16 picohelper!WorkItemFunction+0x86
73d3dda2 clr!UnManagedPerAppDomainTPCount::DispatchWorkItem+0x1d6
73cca42b clr!ThreadpoolMgr::ExecuteWorkRequest+0x4f
73cca319 clr!ThreadpoolMgr::WorkerThreadStart+0x3d3
73d3ed61 clr!Thread::intermediateThreadProc+0x55
749d62c4 kernel32!BaseThreadInitThunk+0x24
77e01f69 ntdll!__RtlUserThreadStart+0x2f
77e01f34 ntdll!_RtlUserThreadStart+0x1b
About this issue
- Original URL
- State: open
- Created 8 years ago
- Reactions: 3
- Comments: 17 (13 by maintainers)
@ismcagdas many thanks - I’m now testing a version with your suggested commits (removed lock). I will report after a few days in production.