Skip to content

fix: count closure calls toward recursion_limit so infinite closure recursion errors instead of crashing - #19229

Merged
fdncred merged 2 commits into
nushell:mainfrom
fdncred:fix_closure_recursion_limit
Oct 14, 2026
Merged

fdncred merged 2 commits into
nushell:mainfrom
fdncred:fix_closure_recursion_limit

Conversation

@fdncred

@fdncred fdncred commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

A closure that calls itself crashes Nushell:

> let M = {|x| do $x $x}
> do $M $M
thread 'main' has overflowed its stack
fatal runtime error: stack overflow, aborting

$env.config.recursion_limit (default 50) exists for this, but only custom command calls added to Stack::recursion_count. A closure call copied the caller's count unchanged, so the check at the top of eval_ir_block never fired for closures. The recursion ran until the Rust stack overflowed and the process aborted.

User-facing changes (Release notes)

Fixed crashes from infinite closure recursion

Fixed a crash when a closure recursed without end. Closure calls (do, each, where, hooks, and so on) now count toward $env.config.recursion_limit, just like custom command calls. Infinite closure recursion now raises a nu::shell::recursion_limit_reached error instead of crashing Nushell with a stack overflow.

For example, let M = {|x| do $x $x}; do $M $M now produces:

Error: nu::shell::recursion_limit_reached

  × Recursion limit (50) reached
   ╭─[source:1:9]
 1 │ let M = {|x| do $x $x}
   ·         ───────┬──────
   ·                ╰── This called itself too many times
 2 │ do $M $M
   ╰────

A recursive custom command that calls itself from inside a closure now uses two levels of the limit per recursion. With the default limit of 50, it stops at 25 levels instead of 50. Raise $env.config.recursion_limit in your config file if you need deeper recursion.

Additional notes

@fdncred fdncred added notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Oct 13, 2026
@fdncred
fdncred merged commit d211796 into nushell:main Oct 14, 2026
11 checks passed
@fdncred
fdncred deleted the fix_closure_recursion_limit branch October 14, 2026 13:51
@github-actions github-actions Bot added this to the v0.117.0 milestone Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant