Ruby backtrace for SystemStackError
mouse 209 · person cloud · link
Last update
2017-07-31
2017
07-31
«how to debug SystemStackError: stack level too deep»

In case of a SystemStackError: stack level too deep exception you do not have a backtrace to inspect, but set_trace_func comes in help!

  • put this in an initializer:
1
2
3
4
5
6
7
8
9
10
11
12
13
# /config/initializers/ruby_tracing.rb
if ENV['RUBY_TRACING']
  $rb_tracing_enable    = false # set to true just before the desired method

  $rb_tracing_file      = File.open(Rails.root.join('rb_tracing.log'), 'w')
  $rb_tracing_file.sync = true

  set_trace_func(proc{|event, file, line, id, binding, classname|
    if $rb_tracing_enable && event == 'call'
      $rb_tracing_file.puts "#{file}:#{line} #{classname}##{id}"
    end
  })
end
  • start the webserver with the RUBY_TRACING env var initialized:
1
RUBY_TRACING=true rails s
  • set the $rb_tracing_enable flag var near the code that explodes:
1
2
$rb_tracing_enable = true
a_method_that_causes_infinite_recursion_in_a_not_obvious_way()
  • inspect the /rb_tracing.log file
1
grep project_path rb_tracing.log

Source: stackoverflow, jbgo's gist