Skip to content

Troubleshooting guide

RecursionError: maximum recursion depth exceeded in Python

Python stops a call chain at 1,000 frames by default. Usually that indicates a bug where a function ends up calling itself by accident.

3 min read10 sectionsUpdated

On this page

Key points

  • The repeating part of the traceback is your cycle.
  • Properties and __getattr__ recurse by accident.
  • Raising the limit rarely fixes the cause.
  • Deep legitimate recursion should become a loop.

What it means

Each call adds a frame, and Python raises RecursionError at the limit as protection against C stack overflow. Python shows repeated traceback lines collapsed: "[Previous line repeated 996 more times]".

Common causes

  • A property setter that assigns to itself, such as setting self.name inside the name setter.
  • __getattr__ reading an attribute that is not set yet.
  • Two functions calling each other.
  • Missing or unreachable base case.
  • Serialising circular references, such as ORM relationships.
  • Decorators applied many times during a reload.

Diagnose it

Read the traceback from the bottom, and find the lines that repeat.

bash
import sys, traceback
print(sys.getrecursionlimit())

try:
    run()
except RecursionError:
    tb = traceback.format_exc().splitlines()
    print("\n".join(tb[:12]))     # the entry point
    print("\n".join(tb[-12:]))    # the repeating cycle

# Faulthandler helps when the interpreter itself crashes
# python -X faulthandler app.py

Fixes

Store the value under another name: self._name within the name property. In __getattr__, use object.__getattribute__ for internal fields, and avoid touching attributes that may not exist yet. Add or fix the base case. Use an explicit stack for walking deep trees. For circular data, exclude back-references or use a serialiser that handles depth. sys.setrecursionlimit is appropriate only when legitimate depth is slightly over the limit.

Rewriting recursion as iteration

Traversing a deep tree recursively can be turned into a loop with a list used as a stack: push the root, then repeatedly pop a node, process it, and push its children. This uses heap memory, which is plentiful, in place of the limited C stack. Python does not perform tail-call optimisation, so a tail-recursive style provides no benefit. Raising the recursion limit too far risks a hard interpreter crash from a real stack overflow, which produces no Python traceback at all and is much harder to debug.

Catch it automatically

Error tracking groups these by the cycle's location, so a single bug appears as one issue with a count and a first-seen release. See OpenTelemetry for Python to send exceptions automatically.

Frequently asked questions

What is the default recursion limit?

It is 1,000.

Is it safe to raise the limit?

A small increase usually is. Very high values can crash the interpreter.

Does Python optimise tail calls?

No.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.