Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get a trace from a 'stack too deep' error in Rails
$enable_tracing = false
#$trace_out = File.open(Rails.root + 'trace.txt', 'w')
set_trace_func proc { |event, file, line, id, binding, classname|
if $enable_tracing && event == 'call'
#$trace_out.puts "#{file}:#{line} #{classname}##{id}"
raise "crash me" if caller_locations.length > 500
end
}
$enable_tracing = true
@bendilley

This comment has been minimized.

Copy link
Owner Author

@bendilley bendilley commented Feb 10, 2015

This trace.rb can go under config/initializers in a Rails app when you need to track down a recursive loop. Note that it's far too inefficient to have it permanently present!

Credit to the top two answers to this stack overflow question, and indeed the original gist.

@ArthurN

This comment has been minimized.

Copy link

@ArthurN ArthurN commented Jul 12, 2015

Thank you.

@mayinx

This comment has been minimized.

Copy link

@mayinx mayinx commented Jul 15, 2019

Bro, I love you ;-) - that saved my day !

@badbabykosh

This comment has been minimized.

Copy link

@badbabykosh badbabykosh commented Jul 10, 2021

I'm a bit of a noob here. So sorry if my question is simple. I've placed the code under config/initializers.
My rails app fails with "stack level too deep" after I try to open my landing page. How would I run this against my app?

I am getting stack level too deep the moment I run my rails app...right as it tries to go to the landing page.

  Processing by MyController#homepage_index as HTML
Completed 500 Internal Server Error in 12ms

SystemStackError - stack level too deep:
  (gem) activesupport-3.1.0/lib/active_support/notifications/instrumenter.rb:24:in `'

How can I use this code to get the trace?

@bendilley

This comment has been minimized.

Copy link
Owner Author

@bendilley bendilley commented Jul 13, 2021

@badbabykosh it's been a long time since I used this initialiser. It should only be necessary if for some reason you haven't got the backtrace already. Stack overflows are nearly always caused by recursive loops, so you should see repeats of the same method sequence over and over in the backtrace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment