Skip to content
Latchkey

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

  1. Read the repeating frames in the backtrace to find the method in the cycle.
  2. Add a base case, or stop the method from calling itself (rename the local, fix the alias, guard the callback).
  3. 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 entirely

How 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →