Friday, June 22, 2012

IIS Worker Process Quits, Service Unavailable

Ran into one of those issue yesterday that I have come across almost 2 years back. My WCF service was hosted on IIS, and we were doing a few load tests, after a few rounds of requests no request went through and was in error.
Tried browsing the WCF SVC file from IIS and got the message "Service Unavailable", went to the application pool section in IIS and noticed that the app pool running the WCF service has stopped.
In a situation like this, I never try to just fix the issue but to see why the problem has come in the first place.
Took a look at the event viewer and I could see a number of errors from ASP.NET and that the worker processor has quit.
Took a look at the code and the code pointed out that their was a piece of code that runs on a separate thread and that thread throws an error due to a missing stored procedure in the database.

So why does the worker processor stop when there is an error?, by default, from .NET 2.0, if there is an unhandled exception, the worker processor quits, but only if this exception is thrown from a thread that does not service the request. If an unhandled exception is thrown from the thread that services the request then its is thrown as a exception to the user.
However, if the exception is thrown from a thread not part of the thread that service the request then it will quit the worker processor.

Any the reason as per Microsoft for this behavior "We do not recommend that you change the default behavior. If you ignore exceptions, the application may leak resources and abandon locks"

So watch out, if you are spawning thread from the ASP.NET request, make sure to handle exception within the thread and log it.