Debugging Stack Level too deep in Ruby
I have ruby program that is running into a stack level too deep (SystemStackError) error, ending on datamapper:
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/collection.rb:510:in `each'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/co开发者_如何学JAVAmparison.rb:616:in `map'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/comparison.rb:616:in `expected'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/comparison.rb:461:in `matches?'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/operation.rb:498:in `matches?'
from /usr/local/lib/ruby/gems/1.8/gems/extlib-0.9.15/lib/extlib/inflection.rb:103:in `any?'
from /usr/local/lib/ruby/gems/1.8/gems/dm-core-1.0.0/lib/dm-core/query/conditions/operation.rb:159:in `each'
from /usr/local/lib/ruby/1.8/set.rb:195:in `each'
... 5188 levels...
Is there a way to debug it? Like looking into the hidden 5188 levels? Ruby-debug was unable to help me, and the builtin ruby profiler dies with "[FATAL] failed to allocate memory"
The problem, besides finding no ruby profiler that can deal with stack overflows, was abusing Datamapper lazyness
The application was getting a text property (which is lazy) inside a query (which is also lazy), inside another query (which is lazy as well). Even though all this lazyness usually turn a N+1 query into a O(k) query (which actually usually means 4 queries), this turned out to generate a stack overflow.
Executing the overflowing test as a simple ruby script, outside rake, rspec and netbeans environment, allowed me to see the culprit more clearly.
Still wished ruby profilers could have helped me with it.
精彩评论