locked
ASP MVC Razor html extension Potential Deadlock with . Task.Result RRS feed

  • Question

  • We have website wir .net 4.5.2 in which we have localizations inside the mongodb. From time to time (days, or weeks) we got hangs of the site.

    We have two scenarios where we use a Task.Result. And this two methods are very often in the threads of the memory dump. Waiting for a monitor.

    In one scenario we need to synchronize a async mongodb read in order to get a text result for a HTML Extension method (which must be synchronous)

    This Methods with Task.Result are ono many threads when the site hangs ( We can see this via MemoryDump and DebugDiag)


    Below the code which raises the problem. We tried it with and without ConfigureAwait(false)

    FindOrAddTextFromRepositoryAsync(key).Result;
    Task.Run(async () => await FindOrAddTextFromRepositoryAsync(key)).Result;
    Task.Run(async () => await FindOrAddTextFromRepositoryAsync(key).ConfigureAwait(false)).Result;

    We can also see in the dump that the problem always only occurs when a GC. IsRunning (Which would explain that it just occurs randomly)

    Also I can see that there is a significant amount of threads waiting for finish gc and another significant amount of threads waiting for Task.Result

    like: 

    [[HelperMethodFrame_1OBJ] (System.Threading.Monitor.ObjWait)] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object
    mscorlib_ni!System.Threading.ManualResetEventSlim.Wait(Int32, System.Threading.CancellationToken)
    mscorlib_ni!System.Threading.Tasks.Task.SpinThenBlockingWait(Int32, System.Threading.CancellationToken)
    mscorlib_ni!System.Threading.Tasks.Task.InternalWait(Int32, System.Threading.CancellationToken)
    mscorlib_ni!System.Threading.Tasks.Task`1[[System.__Canon, mscorlib]].GetResultCore(Boolean)
    SR.BusPortal.Providers.Faciliators.SearchExecutioner.<searchasync>b__e</searchasync>(System.Threading.Tasks.Task`1<sr.busportal.search.statistics.userrequeststatistics>)</sr.busportal.search.statistics.userrequeststatistics>

    Another Code that is in scenario two in the dump is

     foreach (UserRequestStatistics searcherStatistics in tasks.Select(x => x.Result).Where(x => x != null))
                   {
                       statistics.Merge(searcherStatistics);
                   }(edited)

    Now can Task.Result lead to somekind of deadlock and why?

    You can also have a look at this SO questions from 

    http://stackoverflow.com/questions/33628713/potential-deadlock-with-mongodb-2-0-driver-and-non-async-code

    and

    http://stackoverflow.com/questions/33888894/async-methods-in-html-helper-in-asp-net-mvc


    Sunday, December 6, 2015 2:40 PM