Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Async await deadlock problem gone in .NetCore?

In .NetFramework there was a high risk of a deadlock occuring when synchronizing to the synchronization context using:

var result =  asyncMethod().Result; 
var result =  asyncMethod().GetAwaiter().GetResult();

instead of

var result = await asyncMethod();

(read Stephen Cleary blogpost for more info)

Since the synchronization context has been removed in .NetCore. Does this mean that the above methods are now safe to use?

like image 593
PlebFred Avatar asked Nov 12 '18 15:11

PlebFred


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

Yes and no. It's true that there's no synchronization context in .NET Core, and thereby, one of the major sources of deadlock issues has been eliminated. However, this doesn't mean that deadlocks are totally impossible. Regardless, you should not let good programming practices slide, just because it may not be a big issue in one circumstance any more. ASP.NET Core, in particular, is fully async, so there is no reason to ever use a sync version of a method or simply block on an async task. Use await as you always would and should.

like image 75
Chris Pratt Avatar answered Oct 14 '22 22:10

Chris Pratt


You Can Block on Async Code - But You Shouldn’t

The first and most obvious consequence is that there’s no context captured by await. This means that blocking on asynchronous code won’t cause a deadlock. You can use Task.GetAwaiter().GetResult() (or Task.Wait or Task.Result) without fear of deadlock. However, you shouldn’t. Because the moment you block on asynchronous code, you’re giving up every benefit of asynchronous code in the first place. The enhanced scalability of asynchronous handlers is nullified as soon as you block a thread. There were a couple of scenarios in (legacy) ASP.NET where blocking was unfortunately necessary: ASP.NET MVC filters and child actions. However, in ASP.NET Core, the entire pipeline is fully asynchronous; both filters and view components execute asynchronously. In conclusion, ideally you should strive to use async all the way; but if your code needs to, it can block without danger.

-Extract from blogpost by Stephen Cleary

Credit to GSerg for finding the post

However, you might encounter thread pool starvation

http://labs.criteo.com/2018/10/net-threadpool-starvation-and-how-queuing-makes-it-worse/

like image 37
PlebFred Avatar answered Oct 14 '22 21:10

PlebFred