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)

    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


    [[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)

    Another Code that is in scenario two in the dump is

     foreach (UserRequestStatistics searcherStatistics in tasks.Select(x => x.Result).Where(x => x != null))

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

    You can also have a look at this SO questions from 




    Sunday, December 6, 2015 2:40 PM