MvcTurbine, stopping StackOverflowException on w3wp recycle
I had an issue the other day with MvcTurbine where our application would throw a StackOverflowException whenever the worker process recycled.
I love MvcTurbine. If you're working on a asp.net mvc project then you should take a look at it. Having said that I've run into one pretty strange problem with it.
Another developer noticed that there were a lot of eventlog entries on our build server for our projects w3wp process. After looking into it I found that whenever our CI server ran a build the w3wp.exe process would exit with a StackOverflowException.
Strange.
This wasn't a big deal since it wasn't affecting the overall availability of the application, but it was annoying to the dev team.
In visual studio, every time a file is added or a build is done locally the worker-process is recycled which meant that everytime a build was done locally, or a file was added to the project, each dev got the wonderful "Visual Studio Just In-Time Debugger" dialog. Like I said, not a big issue, just very annoying.
After a bit of googling I found that this is indeed an issue with MvcTurbine but it has a very easy fix: just add the following lines to the class that inherits from TurbineApplication:
protected override void ShutdownContext ()
{
CurrentContext = null;
ServiceLocator = null;
}
Javier Lozano - the author of MvcTurbine, explains what's causing the problem:
Since we register the SeviceLocator with itself and we call dispose, essentially it calls dispose on all the types that it has registered, including [itself]. So this causes a recursive loop of calls to ServiceLocator.Dispose... Essentially you're preventing the disposition of the ServiceLocator from [happening] since this piece gets called at App_Shutdown. The fix for v2.2 will not be this, instead it will be addressed at the ServiceLocator registration.
Here are some links where you can see the original post with Javier's answer, take a look at the code that Javier posted showing the fix in place, and the StackOverflow question that should provide some more context.
Let's talk about this