Skip to content
Latchkey

Node "Maximum call stack size exceeded" in CI - Fix Runaway Recursion

This RangeError means the call stack overflowed, almost always from unbounded recursion or a cyclic call chain that never reaches a base case.

What this error means

A node process throws RangeError: Maximum call stack size exceeded with a stack trace that repeats the same function, then exits non-zero.

node
RangeError: Maximum call stack size exceeded
    at serialize (/home/runner/work/app/app/src/serialize.js:4:18)
    at serialize (/home/runner/work/app/app/src/serialize.js:7:12)
    at serialize (/home/runner/work/app/app/src/serialize.js:7:12)

Common causes

Recursion with no reachable base case

A recursive function never satisfies its termination condition, so it calls itself until the stack overflows.

A circular reference during traversal or serialization

An object graph with a cycle drives a recursive walk into an infinite loop.

How to fix it

Add or fix the base case

  1. Identify the repeating frame in the stack trace.
  2. Add the termination condition so recursion stops.
JavaScript
function walk(node) {
  if (!node) return;
  walk(node.next);
}

Track visited nodes to break cycles

  1. Keep a visited set during traversal.
  2. Skip nodes already seen so a cycle cannot recurse forever.
JavaScript
function walk(node, seen = new Set()) {
  if (!node || seen.has(node)) return;
  seen.add(node);
  walk(node.next, seen);
}

How to prevent it

  • Always define a reachable base case, guard traversals against cycles, and convert very deep recursion to iteration so input size cannot blow the stack in CI.

Related guides

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