News for hackers. Stuff that matters.
Re:Asynchronous Programming in C# by torginus on Friday September 24, 2021 @10:35AM (#28642997)
Database operations should be async, you shouldn't consume them synchronously, as they do IO. Async/Await came out in 2012, almost a decade ago (and with great first party library support, I might add).

Moralizing aside, sometimes you do want to call async APIs as synchronous code. I don't think there should be a synchronous version of the API implemented as well, you just need to do

var myValue = DoSomethingAsync().ConfigureAwait(false).Result;

which will avoid deadlocks, and execute your call synchronously

Re:Asynchronous Programming in C# by piedar on Friday September 24, 2021 @01:13PM (#28645216)
> var myValue = DoSomethingAsync().ConfigureAwait(false).Result;

Came out a decade ago and we still don't know how to use it safely.

This example doesn't compile because there is no Result on ConfiguredTaskAwaitable. Regardless, ConfigureAwait(false) does absolutely nothing here because this Task is not being awaited.

If you're going to block this thread, you must push the work to another thread or it's going to deadlock when the implementation tries to resume a continuation (unless the implementation is 100% perfect and the SynchronizationContext smiles upon you).

var result = Task.Run(() => CalculateAsync()).GetAwaiter().GetResult();

This avoids the deadlock but can lead to other nasty things like thread pool starvation. The only true solution is to go async all the way - https://blog.stephencleary.com/2012/07/dont-block-on-async-c...

Re:Asynchronous Programming in C# by sebazzz on Saturday September 25, 2021 @02:39AM (#28651014)
> Regardless, ConfigureAwait(false) does absolutely nothing here because this Task is not being awaited.

It does help if there is a SynchronisationContext active, like in legacy ASP.NET

Re:Asynchronous Programming in C# by rawling on Saturday September 25, 2021 @07:19AM (#28652202)
No, really. ConfigureAwait configures the await. If you don't await - if you block by calling .Result - it does nothing
Re:Asynchronous Programming in C# by myk9001 on Friday September 24, 2021 @01:17PM (#28645274)
>> var myValue = DoSomethingAsync().ConfigureAwait(false).Result;

Doing this inside ASP.NET request processing code (e.g. a controller method) will result in thread pool starvation [1], if you see about 50-100 (the numbers are off the top of my head, so check for yourself) requests per minute hitting that line of code.

P.S.: Sorry for a medium link, but couldn't really find an alternative.

[1]: https://medium.com/criteo-engineering/net-threadpool-starvat...

Re:Asynchronous Programming in C# by spaetzleesser on Friday September 24, 2021 @10:45AM (#28643128)
"var myValue = DoSomethingAsync().ConfigureAwait(false).Result;"

In my view there should be a built-in keyword to do this right. It's too easy to get this wrong and even worse possible problems only show up rarely.

Re:Asynchronous Programming in C# by jtbarrett on Friday September 24, 2021 @11:43AM (#28643946)
If this really needs to happen then I like .GetAwaiter().GetResult() instead of .Result to get the same exception behavior as await rather than the wrapped AggregateException that .Result throws. This is especially helpful if DoSomethingAsync sometimes throws synchronously rather than returning a task.
Re:Asynchronous Programming in C# by gregoryl on Friday September 24, 2021 @11:14AM (#28643555)
The reason there isn't a keyword is because its not possible to do it right. The example given is far from foolproof.
Re:Asynchronous Programming in C# by spaetzleesser on Friday September 24, 2021 @03:41PM (#28646911)
But there should be an easy way to do it right. Needing to call async functions from non-async code is not exactly an unusual thing. Or they need to make absolutely everything async which is also problematic, especially in terms of raw performance.
Re:Asynchronous Programming in C# by torginus on Friday September 24, 2021 @11:10AM (#28643508)
Well, you can put it into an extension method like this:

T SyncResult<T>(this Task<T> task){ return task.ConfigureAwait(false).Result; }

Same for ValueTask. It may already be implemented in the base .NET library as well.

Re:Asynchronous Programming in C# by rawling on Friday September 24, 2021 @11:46AM (#28644006)
To avoid deadlocks, you don't need the ConfigureAwait(false) here, but you do need it to have been applied correctly in all the async code you're calling.
Re:Asynchronous Programming in C# by Joker_vD on Friday September 24, 2021 @11:40AM (#28643916)
Some things simply don't have an async API at the low level, e.g. DNS lookup: there is no asynchronous version of getaddrinfo(3). So if you look at the .NET sources, you'll see that Dns.GetHostEntryAsync pushes a task to a thread pool that calls getaddrinfo(3).

In the end, you arrive at a "sync top-level APIs -- async library APIs -- sync low-level OS APIs" sandwich of dubious efficiency.

Re:Asynchronous Programming in C# by davidfowl on Friday September 24, 2021 @09:16PM (#28649514)
There is on windows. On Linux we queue the requests asynchronously to the same address (golang does similar things)
Re:Asynchronous Programming in C# by chakkepolja on Friday September 24, 2021 @12:43PM (#28644813)
When there's sync OS APIs what's the point of async over threads? I thought async APIs use async or polling version of syscalls and not blocking ones.
Re:Asynchronous Programming in C# by davidfowl on Friday September 24, 2021 @09:18PM (#28649531)
Because there are sometimes better things to do than block more threads. We can asynchronously queue dns requests to the same address (this is what we do in .NET 6)
Re:Asynchronous Programming in C# by ziml77 on Friday September 24, 2021 @02:26PM (#28646090)
There's plenty of database operations I don't do async. We heavily make use of ETLs. By their nature those processes are very linear and don't benefit at all from being async.