SystemStackError: stack level too deep in CI
The Ruby call stack overflowed. Some path recurses without a base case, or two methods call each other endlessly, until the stack limit is hit.
What this error means
A test fails with SystemStackError and a very deep, repetitive backtrace. It can be triggered by a method that accidentally calls itself, an alias_method loop, or a callback that re-triggers itself.
ruby
SystemStackError:
stack level too deep
# ./app/models/account.rb:14:in `balance'
# ./app/models/account.rb:14:in `balance' (x9000)Common causes
Method calls itself
A method references itself (often via super, an accessor, or a same-named local) with no terminating condition.
alias_method or super loop
An aliased method or a module prepend creates a cycle where super resolves back to the same method.
Callback re-triggers itself
An after_save/after_update callback saves the record again, re-firing the same callback indefinitely.
How to fix it
Break the recursion
- Read the repeating frames in the backtrace to find the method in the cycle.
- Add a base case, or stop the method from calling itself (rename the local, fix the alias, guard the callback).
- For callbacks, use a guard or update_column to avoid re-firing.
Guard a self-resaving callback
Ruby
after_save :recalc, unless: :saved_change_to_total?
# or update_column to skip callbacks entirelyHow to prevent it
- Give every recursive method an explicit base case.
- Avoid saving inside save callbacks without a guard.
- Add a focused test for the recursive path.
Related guides
FrozenError: can't modify frozen String in CIFix "FrozenError: can't modify frozen String" in Ruby 3 CI - code mutates a string literal that is frozen, of…
NoMethodError: undefined method for nil:NilClass in CIFix "NoMethodError: undefined method X for nil:NilClass" in Ruby CI - a value you expected was nil, so callin…
Zeitwerk::NameError on autoload in CIFix "Zeitwerk::NameError: expected file to define constant" in Rails CI - a filename does not match the const…