Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is AsyncController grinding my server to a halt?

Tags:

c#

asp.net-mvc

I recently made some (fairly trivial) changes to one of my ASP.NET MVC3 controllers and changed one of the actions into an async action. Basically I took code that looks like this:

public ActionResult MyAction(BindingObject params){
    // use HttpWebRequest to call an external API and process the results
}

And turned it into code that looks like this:

private delegate ActionResult DoMyAction(BindingObject params);

public void MyActionAsync(BindingObject params){
    AsyncManager.OutstandingOperations.Increment();
    var doMyAction = new DoMyAction(MyAction);
    doMyAction.BeginInvoke(params, MyActionCallback, doMyAction);
}

private void MyActionCallback(IAsyncResult ar){
    var doMyAction = ar.AsyncState as DoMyAction;
    AsyncManager.Parameters["result"] = doMyAction != null ? doMyAction.EndInvoke(ar) : null;
    AsyncManager.OutstandingOperations.Decrement();
}

public ActionResult MyActionCompleted(ActionResult result){
    return result;
}

private ActionResult MyAction(BindingObject params){
    // use HttpWebRequest to call an external API and process the results
}

This seems to work fine, when I test it locally calling MyAction, breakpoints in each of the methods fire when I would expect them to and it ultimately returns the expected result.

I would anticipate this change to at best improve performance under heavy load because now my worker threads aren't being eaten up waiting for the HttpWebRequest to call the external API, and at worst have no effect at all.

Before pushing this change, my server's CPU usage averaged around 30%, and my W3SVC_W3WP Active Requests perfmon stat hovers around 10-15. The server is Win Server 2008 R2 and the MVC site gets around 50 requests per second.

Upon pushing this change, the CPU shoots up to constant 90-100% usage, and the W3SVC_W3WP Active Requests counter slowly increases until it hits the maximum of 5000 and stays there. The website becomes completely unresponsive (either timing out or giving "Service Unavailable" errors).

My assumption is I'm either implementing the AsyncController incorrectly, missing some additional configuration that's required, or maybe just misunderstanding what the AsyncController is supposed to be used for. In any case, my question is why is this happening?

like image 374
Rudism Avatar asked Dec 01 '25 14:12

Rudism


1 Answers

By async-invoking a delegate you move the work to the thread pool. You still burn a thread. You gain nothing and loose performance.

Async mostly makes sense when you can trigger true async IO.

like image 80
usr Avatar answered Dec 03 '25 04:12

usr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!