blocking on async code
Calling .Result or .Wait() on a task so that a thread sits idle until the async work finishes.
Blocking on tasks can hang or starve a server under load.
In more detail
Under load it starves the thread pool, and where a synchronization context exists it can deadlock outright. Await the task instead, all the way up.
Related terms
- deadlock — A state where two or more pieces of work each wait for the other forever, so none can continue.
- thread pool starvation — A state where every pool thread is blocked waiting, so new work queues up and the server slows to a crawl though the processor is idle.
- async and await — Language keywords for writing code that waits for slow work, such as a network call, without blocking a thread.
- synchronization context — An object that decides which thread an await resumes on, used by user-interface frameworks and older ASP.NET.
Taught in
- Async and Await: Managing Concurrency — Modern .NET Core, C# & Enterprise Microservices
- Querying, Change Tracking, and Saving Data — Modern .NET Core, C# & Enterprise Microservices