How to track asynchronous / pending deadlocks in MVC4?

After reading "Best Practices in Asynchronous Programming," I decided to test the behavior of the deadlock in MVC4. After creating a website from an Intranet template, I changed the index action as follows:

using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; namespace AsyncAwait.MVC4.Controllers { public class HomeController : Controller { private static async Task DelayAsync() { await Task.Delay(1000); } // This method causes a deadlock when called in a GUI or ASP.NET context. public static void Test() { // Start the delay. var delayTask = DelayAsync(); // Wait for the delay to complete. delayTask.Wait(); } public ActionResult Index() { ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; Test(); return View(); } } } 

The call to the index freezes as I expected, but I also expected that at some point an exception would be thrown. An exception is never thrown and all requests just hang.

I looked at all the available performance counters and could not figure out how to identify the deadlock. If I were working with an existing website that uses async / wait, how can I configure monitoring for possible deadlocks?

Thanks!

+4
source share
1 answer

If you expect your tasks to be completed within the predicted time frame, you can use timeouts.

Task.Wait has a couple of overloads that take a timeout value.

If, for example, your task should not take more than 5 seconds, you can do something like this.

 var delayTask = DelayAsync(); // Will be true if DelayAsync() completes within 5 seconds, otherwise false. bool callCompleted = delayTask.Wait(TimeSpan.FromSeconds(5)); if (!callCompleted) { throw new TimeoutException("Task not completed within expected time."); } 
0
source

All Articles