Is .GetAwaiter().GetResult(); safe for general use?

27,041

As I describe on my blog, GetAwaiter().GetResult() can deadlock when it's used in a one-thread-at-a-time context. This is most commonly seen when called on the UI thread or in an ASP.NET context (for pre-Core ASP.NET).

Wait has the same problems. The appropriate fix is to use await, and make the calling code asynchronous.

Note that the Main method in Console apps is an exception to this rule; it is perfectly appropriate to use there. Many code samples use it in this way.

Share:
27,041
Cyan
Author by

Cyan

About you

Updated on March 12, 2020

Comments

  • Cyan
    Cyan about 4 years

    I read in a few places that .GetAwaiter().GetResult(); could cause deadlocks and that we should use async/await instead. But I see many code samples where this is used. Is it ok to use it? Which are the cases where it can deadlock? Is there something else I should use, like Task.Wait?

  • Cyan
    Cyan almost 8 years
    I think this is also true for WebApi services, correct?
  • Stephen Cleary
    Stephen Cleary almost 8 years
    @Cyan: Yes, any kind of ASP.NET request context. Pre-Core, that is; the request context was removed in ASP.NET Core.
  • Teoman shipahi
    Teoman shipahi over 7 years
    "Main method in Console apps is an exception to this rule; it is perfectly appropriate to use there. Many code samples use it in this way." has a perfect point here. Thanks!
  • Martin
    Martin almost 7 years
    @stephenCleary would the comment about main methods apply to methods fired from within a thread started from ThreadPool.QueueUserWorkItem?
  • Stephen Cleary
    Stephen Cleary almost 7 years
    @Martin: You want to block the main thread in a Console app because otherwise the application exits. Why would you want to block a thread pool thread?
  • Martin
    Martin almost 7 years
    @StephenCleary best answer is "reasons", we fire off a thread per "thing", and that shares some code that Async as used in a web solution... would prefer not to recreate the code as Sync if I can help it...
  • Stephen Cleary
    Stephen Cleary almost 7 years
    @Martin: I assume you mean that you're using thread-local state or some other mechanism that requires a dedicated thread, and that you don't want to make the code asynchronous. In that case, sure, just use GetAwaiter().GetResult(). I'm assuming your dedicated thread is contextless, so it shouldn't deadlock.
  • BrewMate
    BrewMate over 6 years
    @StephenCleary, what about Azure Functions? .Net Core is only in preview if you select the beta runtime version in App Settings. Is that the better option?
  • Stephen Cleary
    Stephen Cleary over 6 years
    @BrewMate: Azure Functions always runs your code without a context, both in ASP.NET Core and ASP.NET Classic.
  • Steven.Xi
    Steven.Xi about 6 years
    @StephenCleary , in your opinion, is there an way to run a async method sync safely? Thanks.
  • Stephen Cleary
    Stephen Cleary about 6 years
    @Steven.Xi: Not in the general case. In specific scenarios, it can be done. See msdn.microsoft.com/en-us/magazine/mt238404.aspx