I talk a lot about antipatterns and how to detect and avoid them, I have specifically taken to referring to sync-over-async as my arch nemesis!
I created this video as part of the launch for Visual Studio 2022 to lay out some guidance with examples of good and bad patterns. I show the tools I have used to capture diagnostics artifacts that can help with detection and remediation.
While in this video I suggest using Threading Analyzer nuget packages the Roslyn analyzer in .NET 6 has some significant improvements for detect async anti-patterns. Happy debugging!
Is that true if you're calling Task.Delay(...) under the covers? I believe that's implemented using a timer, so that there is no "other" thread.
Even in this case we still suffer from thread starvation - just because we're unnecessarily blocking the initiating thread while we wait for the result.
Interesting, my assumption was that Task continuations are always scheduled against the thread pool and so that led to me thinking even Task.Delay() would consume an extra thread pool thread. To be sure my underlying assumption was that in reality there would be some "real" Task being done by DoAsyncOperationWell().
Thanks!
Good reminder for everyone! FYI in NET 6 there is an interesting App Context switch that helps with addressing some of the downsides of sync over async https://github.com/dotnet/runtime/blob/a7a2fd6543ff71cecbbfe901b81ee27a6cf428c0/src/libraries/System.Private.CoreLib/src/System/Threading/PortableThreadPool.Blocking.cs#L271-L314
I'm not implying this means we should not do what you suggested. Yet it gives us some nice perf gains for the cases that are simply not under our control like a third party lib that misbehaves
Happy Christmas time
Daniel
Comments are closed.