I am seeing a pretty crazy error crop up when using the MVC MiniProfiler. Intermittently, the site I'm working on enters a state where every request results in this exception being thrown:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.TypeInitializationException: The type initializer for 'MvcMiniProfiler.MiniProfiler' threw an exception.
---> System.Threading.LockRecursionException: Write lock may not be acquired with read lock held.
This pattern is prone to deadlocks. Please ensure that read locks are released before taking a write lock.
If an upgrade is necessary, use an upgrade lock in place of the read lock.
at System.Threading.ReaderWriterLockSlim.TryEnterWriteLockCore(Int32 millisecondsTimeout)
at System.Threading.ReaderWriterLockSlim.TryEnterWriteLock(Int32 millisecondsTimeout)
at System.Web.Routing.RouteCollection.GetWriteLock()
at MvcMiniProfiler.UI.MiniProfilerHandler.RegisterRoutes()
in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\UI\MiniProfilerHandler.cs:line 81
at MvcMiniProfiler.MiniProfiler..cctor()
in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\MiniProfiler.cs:line 241
— End of inner exception stack trace —
at MvcMiniProfiler.MiniProfiler.get_Current()
at TotallyNotOverDrive.Boom.MvcApplication.Application_EndRequest()
The error persists until the app pool is recycled. Looks like somehow a lock is being held which prevents the MiniProfiler from trying to register it's routes. This occurs for requests where I am not starting the MiniProfiler, but during Application_EndRequest I call MiniProfiler.Stop(), which seems to result in a MiniProfiler being created when the Current property is accessed. For a simple solution, I modified EndRequest to use the same logic for stopping the profiler as BeginRequest, so if the request is not using the profiler this error should be avoided completely. I would still like to resolve the actual problem before sending this code to production.
My route table is pretty simple, and is only added to within the Application_Start method. We are not using any other third-party code which might be modifying the route table after startup. The only suspect thing I've done with routing is add a custom Route to the table, but it's a pretty straightforward route, I just needed some more complicated pattern matching than a standard MVC route could accomplish.
I looked through the relevant MiniProfiler code and don't see anything that could be causing a lock to go unreleased, so I'm assuming it's a combination of ASP.NET and MiniProfiler conflicting when accessing the RouteTable. I can't reliably reproduce the problem, so I'm wondering if anyone else has had problems like this with routing. Thanks for any help you can offer.
I think I see what is happening, you need to get the routes registered earlier. You are registering them during EndRequest, at this point there may be other requests in the pipeline which are holding read locks on the route table.
The routes are registered in the static constructor of MiniProfiler. If you access any of the settings in MiniProfiler during Application_Start, this will kick off the static constructor that will register the routes.
For example, try adding something like this, just after registering your routes.
MiniProfiler.Settings.PopupMaxTracesToShow = 10;
Related
I'm using Ninject in an ASP MVC project for database binding. In "private static IKernel CreateKernel()", I'm binding a database object like below:
kernel.Bind<IDbSession>().ToProvider<IDbSession>(new NhDbSessionProvider()).InRequestScope();
This works almost exactly as intended, but some pages on the site use AJAX calls to methods on the controller and it seems like every one of these calls opens a SQL connection and doesn't dispose of it when the controller returns. Eventually this causes the NumberOfConnections on the SQL database to exceed the max and the site throws errors that connections aren't available.
I'm pretty new to Ninject and taking over an existing project trying to make this work without doing major changes. Any ideas what I can do to get these objects to dispose? It seems like they should be automatically doing this already from what I'm reading, but maybe I just don't understand.
If I need to share more code let me know.
Disposable instances are Disposed at end of request processing, according to the documentation.
However, the documentation for InRequestScope has a section on Ensuring Deterministic Dispose calls in a Request Processing cycle.
Specifically:
Use the Ninject.Web.Common package
Manually registering OnePerRequestModule in web.config (e.g. if you're
not using a Ninject.Web.MVC* NuGet Package)
It is perhaps the case that your instances are being/will be Disposed, but just not at the time you're expecting them to be.
Seems someone commented out this line for whatever reason (it wasn't me). Adding it back resolves the problem.
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
I have been searching the web for the issue I'm facing for quite some time now. I found people facing it, but couldn't get an appropriate solution to the problem. The issue is what would be the best way to handle any exception occurred in an template.gsp ? I know I can use a try-catch there so that my controller don't get the exception, but I did find people saying its not a good practice, but failed to answer why. So is it a wrong way and if it is then is there any better solution to deal with this problem ?
The correct way is with a 500 error handler: http://grails.org/doc/latest/guide/theWebLayer.html#mappingToResponseCodes
If you have a lot of logic in your views that could produce a error consider refactoring the code into a tag library which can incorporate better error handling
It depends on what behavior you want for your application with the error.
If a generic error page works for the application - then setup an error page in URL mappings and ignore the error in the controller.
Do you need a nice custom error that is specific for that case - (or possibly display an alternate page? ), if so then you'd need to a try catch (or do some fun with URL mapping and creating an error controller). The objections that come up in Exception handling in Grails controllers arise around the amount of code required for the error handling in the controller vs the rest of the application - with the error handling code being ~40% of each controller method. This causes the code to seem bad/bloated (and apparently violate the CodeNarc "CatchException" rule).
I'm doing BDD on an MVC3 project with SpecFlow. My current specification scenario says that:
Given a user is working on the system
When an error is raised
Then the user should be redirected to error page
And display a link to go back where he came from
How can I test a spec like this? I usually test the controller directly, however the Error view given by the standard MVC3 template has no controller, and no controller is used, because is redirected by the HandleError global filter.
In exceptional cases I use Watin to test that the behavior conforms to what the specification says, however to do that I need a view that raises an error, something that when everything is working i do not have.
Any ideas on testing scenarios like this?
I have a few thoughts on this scenario:
1.) "Given a user is working on the system" is a pretty vague step. What code would be found in the step definition? Unless you have a user class that has a WorkingOnSystem method, it might be worth taking this line out.
2.) Without having seen the rest of your code, I think the target of this feature should be the HandleError filter itself. By its very definition, you know that when it is invoked an error has occurred. All you need to do is instantiate the filter, call the appropriate method, and test the results.
Think about it this way: What does "When an error is raised" mean in your system? If your HandleError filter is not the place, then you probably don't have a place. In which case, you'll need to be more specific.
I think the awkwardness around this spec is due to ASP.Net MVC. When you're dealing with a framework of abstractions, you're sometimes left to "wrap" your specs around some part of it. We just can't go end-to-end easily when the parts of the application come from so many places.
I'm looking for some smart ideas on how to quickly find all usage of session state within an existing asp.net (MVC) application.
The application in question was the subject of outsourced development, and has been running fine in production. But we recently realised that it's using InProc session state rather than (our preferred route) StateServer.
In a small scale test, we switched it over to StateServer, and all worked fine. However, when we deployed to production, we suddenly experienced a large number of errors. I'm not sure if these errors were caused by this change (they were actually complaining about database level problems), but removing the change allowed the application to function once again (this may have just been because it caused a recycle to occur).
So before I try switching it again, I'd like to perform a thorough audit of all objects being placed in the session (I'd previously taken a quick look at a couple of controllers, and they seemed fine). If I had full control over the code, this is the kind of place where I'd just comment out the class (to compile and find the various ways of reaching the session class), then comment out the accessors, hit compile, and visit each error. But I can't do that with built in .NET framework types.
So, any smart ideas on how to find each usage?
I decided to try using Reflector. I've analyzed the "Used By" for each of the following:
System.Web.HttpSessionStateBase.set_Item(String, Object) : Void
System.Web.SessionState.HttpSessionState.set_Item(String, Object) : Void
System.Web.SessionState.HttpSessionState.set_Item(Int32, Object) : Void
System.Web.HttpSessionStateBase.set_Item(Int32, Object) : Void
System.Web.HttpSessionStateBase.Add(String, Object) : Void
System.Web.SessionState.HttpSessionState.Add(String, Object) : Void
(and checked that we don't use TempData anywhere). Am I missing any other routes by which items can end up in the Session?
You can get the source for asp.net MVC. to look for use of Session
http://aspnet.codeplex.com/releases/view/58781 for MVC 3
http://aspnet.codeplex.com/releases/view/41742 for MVC 2
http://aspnet.codeplex.com/releases/view/24471 for MVC 1
I've actually found these quite useful to have laying around for when you need to find out why something is doing what it does.
MVC 2 won't run with Session switched off, so it may be using the session in ways not compatible with stateserver.
From the DB errors, sounds like nHibernate maybe doing something. You could get the source for that as well to have a look see, but I'm sure it's use of session will be documented.
Simon
I say do a solution wide search for "Session". Besides this if this is ASP.Net MVC application, then don't forget that TempData is also Session.
I was debugging my current project (ASP.net MVC 1.0 project)
and stumbled upon slightly disturbing behavour.
It seems that when the router is hunting for a referenced partial view
aka
<%Html.RenderPartial("AccountListControl", ViewData["AccountList"]); %>
It cycles through it's default locations until it finds the correct spot.
So it checks "Views\Shared\AccountListControl"
and checks "Views\Home\AccountListControl"
etc
Once it finds a match - all is good.
Bad locations are identified by the web exception thrown in each case.
Is there a significant performance cost for all of these exceptions?
Should I modify the code to be more explicit?
It seems that in Release mode there are no exceptions thrown and view locations are cached so there is no need to be more explicit.