deadlock even after using ConfigureAwait(false) in Asp.Net flow

后端 未结 3 1259
无人及你
无人及你 2020-12-20 12:29

I\'m hitting deadlock even after using ConfigureAwait(false), below is the sample code.

As per the sample http://blog.stephencleary.com/2012/02/async-a

相关标签:
3条回答
  • 2020-12-20 13:10

    From the comments:

    I was under assumption, once ConfigureAwait(false) is used (any where in the call stack), execution from that point will not cause deadlock.

    I don't believe in black magic, and neither should you. Always strive to understand what happens when you use something in your code.

    When you await an async method that returns a Task or a Task<T>, there is an implicit capture of the SynchronizationContext by the TaskAwaitable being generated by the Task.GetAwaiter method.

    Once that sync context is in place and the async method call completes, the TaskAwaitable attempts to marshal the continuation (which is basically the rest of the method calls after the first await keyword) onto the SynchronizationContext (using SynchronizationContext.Post) which was previously captured. If the calling thread is blocked, waiting on that same method to finish, you have a deadlock.

    You should ask yourself Should I expose synchronous wrappers for asynchronous methods? 99 percent of the time the answer is no. You should use a synchronous API, such as the one WebClient offers.

    0 讨论(0)
  • 2020-12-20 13:12

    It blocks when used in ProjectsRetriever because:

    public class ProjectsRetriever
    {
        public string GetProjects()
        {
            //querying the result blocks the thread and wait for result.
            var projects = this.GetProjects(uri).Result;
            ... //require Thread1 to continue.
            ...
        }
    
        private async Task<IEnumerable<Project>> GetProjects(Uri uri)
        {
            //any thread can continue the method to return result because we use ConfigureAwait(false)
            return await this.projectSystem.GetProjects(uri, Constants.UserName).ConfigureAwait(false);
        }
    }
    
    public class ProjectSystem
    {
        public async Task<IEnumerable<Project>> GetProjects(Uri uri, string userName)
        {
            var projectClient = this.GetHttpClient<ProjectHttpClient>(uri);
            var projects = await projectClient.GetProjects();
            // code here is never hit because it requires Thread1 to continue its execution
            // but Thread1 is blocked in var projects = this.GetProjects(uri).Result;
            ...
    }
    

    It does not block when used in ProjectSystem because:

    public class ProjectsRetriever
    {
        public string GetProjects()
        {
            ...
            var projects = this.GetProjects(uri).Result;
            ...//requires Thread1 to continue
            ...
        }
    
        private async Task<IEnumerable<Project>> GetProjects(Uri uri)
        {
            //requires Thread1 to continue
            return await this.projectSystem.GetProjects(uri, Constants.UserName);
        }
    }
    
    public class ProjectSystem
    {
        public async Task<IEnumerable<Project>> GetProjects(Uri uri, string userName)
        {
            var projectClient = this.GetHttpClient<ProjectHttpClient>(uri);
            var projects = await projectClient.GetProjects().ConfigureAwait(false);
            // no deadlock, resumes in a new thread. After this function returns, Thread1 could continue to run
    }
    
    0 讨论(0)
  • 2020-12-20 13:12

    I had the same problem. "ConfigureAwait(false)" can not always avoid dead lock.

    public class HomeController : Controller
    {
        public async Task<ActionResult> Index()
        {
            // This works !
            ViewBag.Title = GetAsync().Result;
    
            // This cause deadlock even with "ConfigureAwait(false)" !
            ViewBag.Title = PingAsync().Result;
    
            return View();
        }
    
        public async Task<string> GetAsync()
        {
            var uri = new Uri("http://www.google.com");
            return await new HttpClient().GetStringAsync(uri).ConfigureAwait(false);
        }
    
        public async Task<string> PingAsync()
        {
            var pingResult = await new Ping().SendPingAsync("www.google.com", 3).ConfigureAwait(false);
    
            return pingResult.RoundtripTime.ToString();
        }
    }
    

    For the above code, "GetAsync()" works while "PingAsync()" doesn't.

    But I found that if I wrap the async call into a new task, and wait this task, PingAsync() will work event without "ConfigureAwait(false)":

    var task = Task.Run(() => PingAsync());
    task.Wait();
    ViewBag.Title = task.Result;
    

    I don't know the reason, maybe someone can tell me the difference.

    0 讨论(0)
提交回复
热议问题