The call stack: the whole lesson in words
One customer's support thread is a blank page. Every other thread loads.
Ana's blank page
Ana: I open my support thread, and the page goes blank.
Dev: Only yours? The console shows one line: RangeError, maximum call stack size exceeded.
A crash for one customer's data points at that data meeting the code.
How deep?
Dev: This is the code that draws a thread.
Agent: I wrote it. It draws a comment, then calls itself for each reply.
Dev: Let me run it on a normal one first.
Afterdiff: While the deepest reply is being drawn, how many renderReplies calls are still running?
One frame
Afterdiff: The script calls renderReplies, and the call gets a frame: a block on the stack that keeps its comment and its place in the code. It prints Ana's first message.
Every call gets its own frame on the call stack.
A call waits
Afterdiff: c2 is a reply to c1, so renderReplies calls itself. A new frame goes on top, and the reply prints indented.
Dev: So the c1 call hasn't finished?
Afterdiff: No. It waits at line ten.
A call that calls another is paused, not finished.
Three deep
Afterdiff: c2 has a reply too. A third frame lands, and order 1042 prints one level further in.
Dev: Three frames, three indents. The page shows the stack.
How deep a row is indented is how deep its call is on the stack.
Read the trace
Dev: And a stack trace?
Afterdiff: It is this stack at the moment something throws. The top line is where it broke. Each line below is the call that led there.
A stack trace is the call stack at the moment of the error, newest frame first. Chrome keeps the top 10 frames by default.
Which ends first?
Afterdiff: Three calls are running. Which one returns first?
Last in, first out
Afterdiff: c3 returns and its frame comes off. Then c2. The c1 call carries on, draws c4, and returns last.
Dev: Last in, first out.
The last call in is the first out.
The bad record
Dev: Ana's thread came from an import. Her first message names its own reply as its parent.
Afterdiff: So c1 draws c2, and c2 draws c1, forever. At about five thousand calls, Chrome stops the script.
A cycle in the data is recursion that never returns.
The retry
Dev: Can you fix the crash?
Agent: Done. I wrapped the loop in try and catch. If it fails, I retry with the same comment.
Ana: Now my page is stuck.
Agent: Same input, same calls. My retry never ends. Twenty seconds later, Chrome was still running it.
Retrying the same call repeats the same calls.
The fix
Dev: Remember which comments you have drawn, and skip one you have seen.
Afterdiff: c2 finds c1 already drawn and returns at once. Every comment draws once, and the stack empties.
Ana: It loads now.
Agent: Fix the cycle, not the error.
Remember what you have drawn: a comment seen once returns at once.
The rule
A call stays on the stack until every call it made returns. A normal thread returns. A cycle never does, and retrying repeats it. Stop the cycle, and the stack empties.
A frame stays until every call it made has returned.
The common wrong idea
A function call runs and finishes before the next line, so calls made inside it are separate and only one call is ever open.
What really happens
Each call gets a frame on the call stack and stays there, paused, until every call it made has returned. The last call in is the first out.
The one-page cheatsheet for this lesson →