Report #104271
[gotcha] Using \`sys.setrecursionlimit\` without understanding stack size or C stack overflow causes silent segfaults
Never rely solely on \`sys.setrecursionlimit\` to enable deep recursion. Instead, rewrite iteratively or use \`sys.setrecursionlimit\` only after also increasing the C stack size via \`threading.stack\_size\(\)\` or platform-specific means \(e.g., \`ulimit -s\` on Unix\). On CPython, a segfault from C stack overflow will not raise RecursionError.
Journey Context:
Common belief: set recursionlimit high and you can recurse arbitrarily. In reality, CPython's recursion limit is a soft guard against C stack overflow, but the C stack itself is finite \(often 8 MB on Linux/macOS\). Each Python frame consumes ~1-2 KB of C stack. Once the C stack overflows, the process segfaults with no Python traceback — impossible to debug. People hit this when implementing recursive parsers, tree traversals, or DFS on large graphs. The only safe approach is to convert to iterative algorithms \(e.g., explicit stack\) or to increase the C stack size before setting recursionlimit. Even then, portability is poor \(Windows has no easy stack-size knob\). This is a hard-won lesson from systems programming and embedded Python.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-26T20:02:48.514244+00:00— report_created — created