Skip to content
Latchkey

Python "RecursionError: maximum recursion depth exceeded" in CI

Python caps the call stack to avoid crashing the interpreter, and your call chain exceeded it. Either the recursion has no terminating base case, or it is legitimately deep and the default limit is too low.

What this error means

A run fails with "RecursionError: maximum recursion depth exceeded" - sometimes "while calling a Python object" or during pickling/comparison. The traceback repeats the same frames.

python
  File "app/tree.py", line 12, in walk
    return walk(node.parent)
  File "app/tree.py", line 12, in walk
    return walk(node.parent)
RecursionError: maximum recursion depth exceeded

Common causes

Missing or unreachable base case

A recursive function never hits its stopping condition - e.g. a cyclic parent reference - so it recurses until the limit.

An accidental property/__getattr__ loop

A property or __getattr__ that references itself recurses on every access until the depth limit trips.

How to fix it

Add or fix the base case

  1. Read the repeated frames in the traceback to find the recursive call.
  2. Ensure the function returns without recursing when the terminating condition holds.
  3. Guard against cycles (e.g. a visited set) if the data can loop.
app/tree.py
def walk(node):
    if node is None:
        return None
    return walk(node.parent)

Raise the limit for genuinely deep recursion

If the recursion is correct but deep, raise the limit deliberately - or convert it to an iterative loop.

app/main.py
import sys
sys.setrecursionlimit(10000)

How to prevent it

  • Always provide a reachable base case in recursive functions.
  • Guard against cyclic data with a visited set.
  • Prefer iteration for deep traversals that could exceed the limit.

Related guides

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