05 / 08

A production script fails intermittently with a RecursionError. How would you diagnose and fix it?

Difficulty: 8/10
RecursionError, Debugging, Stack Depth

Reproduce, capture the traceback, find the cycle, then convert recursion to iteration or raise the limit deliberately

RecursionError is raised when the interpreter exceeds sys.getrecursionlimit() during a call. Intermittent cases usually mean the recursion depth depends on input size or on a rare data shape such as a cycle in a graph or a self-referential object. The first step is to capture the traceback, because the repeated frame in the traceback points directly at the recursive function. Then reproduce with the smallest input that triggers it. The fix is almost always to convert the recursion into an explicit stack or a loop, because raising the limit increases the risk of a C stack overflow that crashes the process hard instead of raising a clean Python error. Only raise the limit if you have measured that the depth is bounded and the process can afford the stack.

  1. 1

    Read the traceback: the frame that repeats is the recursion site. That is your starting point.

  2. 2

    Check for cycles: a self-referential structure or a graph with a back edge will recurse forever.

  3. 3

    Reproduce with minimal input, then reduce further until the trigger is obvious.

  4. 4

    Prefer converting to an iterative algorithm with an explicit stack. It is bounded and predictable.

  5. 5

    If you must raise the limit, do it in the entry point and treat it as a temporary measure, not a fix.

  6. 6

    Common mistake: raising sys.setrecursionlimit(100000) and then seeing a segmentation fault instead of a clean error.

  7. 7

    Version note: sys.setrecursionlimit exists in all versions, and 3.12 improved some recursion-related error messages, but the underlying C stack risk is unchanged.

Scenario Questions

0-2 years experience

  1. 1You see the same function name repeated in the traceback. What does that tell you?
  2. 2Why is RecursionError better than a crash but still a real bug?

2-5 years experience

  1. 1A tree-traversal function works on small inputs and fails on one customer's data. How do you reproduce and fix it?
  2. 2A JSON-like parser hits RecursionError on deeply nested input. How do you bound depth without changing the algorithm?

5-8 years experience

  1. 1You are processing a user-supplied graph and cannot trust input depth. How do you design the traversal to be safe and bounded?
  2. 2Your recursive serializer hits the limit on large object graphs with shared references. How do you convert to iterative and handle cycles?

8+ years experience

  1. 1Design a schema validator that handles arbitrary nesting safely with a bounded, iterative algorithm and clean error reporting.
  2. 2Explain how tail calls, trampolining, and generators can replace recursion in Python, and where each approach fits best.

Follow-up Questions

  • Why can raising sys.setrecursionlimit lead to a segmentation fault?
  • How would you detect a reference cycle that is causing infinite recursion?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.