Long delays on NameError in Rails views

After upgrading to Rails 3.1, whenever we made a typo in a method name in the view, we experienced a long delay before the error was displayed, or the request eventually timed out. If we killed the server, we had a cryptic error message:

Mysql2::Error: This connection is still waiting for a result, try again once you have the result: SHOW TABLES

After some ruby-prof-ing I found out that most processing takes place under NameError#message in ActionDispatch::Routing::RouteSet.

This is what happened:

To add insult to injury, the result of inspect was shortened to display:

"undefined local variable or method idontexist' for #<#<Class:0x1078ad628>:0x10789d368>

If you include <%= self.inspect %> in your view, you get the same result.

If you override ActionDispatch::Routing::RouteSet#inspect with a simple method, you don’t get any delay.

I have created a gem called short_inspect that makes sure that inspect will not be called recursively. Follow the link to see how to use it.

Please let us know in the comments if you have experienced this problem and if this gem fixed it or you have found a better solution.

Posted on 23 November 2011 by Levente @leente.
blog comments powered by Disqus