Reputation: 309
I have an ASP.NET app. I'm load testing it and I managed to kind of block all threads from thread pool by running concurrent requests using an external tool. All threads are blocked due to an I/O request over the network and the method which is doing so is implemented with async and await keywords. I also have another method in another controller which just returns a string, that is, this second method does not do I/O.
The question is: isn't all requests to this non-blocking method supposed to be served even if all threads are doing asyn I/O ?
Upvotes: 1
Views: 162
Reputation: 241450
One can still block when calling a method that is implemented as async.
public async Task GetFooAsync()
{
await DoSomethingElseAsync();
}
public static void MySyncMethod()
{
GetFooAsync().Wait();
}
By calling .Wait()
, I'm blocking the thread. It doesn't matter that the method is implemented using async/await. The same thing happens with .Result
Async methods need to be used "async all the way" - meaning the entire call stack must be async in order to be non-blocking. In an ASP.NET app, that means you are starting with an async method in your controller. If the controller method is synchronous, calling an async method will still be blocking.
See "async all the way" in this MSDN article. You may also wish to watch these essential tips for async/await on Channel 9.
Upvotes: 4