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
This is what happened:
NameErrorwas raised in a view
messagemethod was called on it several times; and each time:
inspectmethod was called on the class where the error was raised, i.e. the one that’s
selfin the view
inspecton every instance variable
ActionDispatch::Routing::RouteSetwas encountered, and that resulted in an iteration over all routes, which took a long time (
routes.rbis ~300 lines)
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