Skip to content

Enforce call stack depth limit for all calls - #11729

Merged
WindSoilder merged 1 commit into
nushell:mainfrom
TrMen:general_recursion_limit
Feb 7, 2024
Merged

WindSoilder merged 1 commit into
nushell:mainfrom
TrMen:general_recursion_limit

Conversation

@TrMen

@TrMen TrMen commented Feb 4, 2024 •

Copy link
Copy Markdown
Contributor

Description

Previously, only direcly-recursive calls were checked for recursion depth. But most recursive calls in nushell are mutually recursive since expressions like for, where, try and do all execute a separte block.

def f [] {
    do { f }
}

Calling f would crash nushell with a stack overflow.

I think the only general way to prevent such a stack overflow is to enforce a maximum call stack depth instead of only disallowing directly recursive calls.

This commit also moves that logic into eval_call() instead of eval_block() because the recursion limit is tracked in the Stack, but not all blocks are evaluated in a new stack. Incrementing the recursion depth of the caller's stack would permanently increment that for all future calls.

Fixes #11667

User-Facing Changes

Any function call can now fail with recursion_limit_reached instead of just directly recursive calls. Mutually-recursive calls no longer crash nushell.

After Submitting

Previously, only direcly-recursive calls were checked for recursion
depth. But most recursive calls in nushell are mutually recursive since
expressions like `for`, `where`, `try` and `do` all execute a separte block.

```nushell
def f [] {
    do { f }
}
```
Calling `f` would crash nushell with a stack overflow.

The only general way to prevent such a stack overflow is to enforce a
maximum call stack depth instead of only disallowing directly recursive
calls.

This commit also moves that logic into `eval_call()` instead of
`eval_block()` because the recursion limit is tracked in the `Stack`, but
not all blocks are evaluated in a new stack. Incrementing the recursion
depth of the caller's stack would permanently increment that for all
future calls.

Fixes nushell#11667
@TrMen
TrMen force-pushed the general_recursion_limit branch from 3a2c673 to 8893248 Compare February 4, 2024 08:36
@fdncred

fdncred commented Feb 4, 2024

Copy link
Copy Markdown
Contributor

From my testing, this seems to work great!

@WindSoilder
WindSoilder merged commit 4b91ed5 into nushell:main Feb 7, 2024
@fdncred fdncred added the notes:fixes Noted in "Bug fixes" section label Feb 7, 2024
@fdncred fdncred added this to the v0.91.0 milestone Feb 7, 2024
@sholderbach

Copy link
Copy Markdown
Member

Thanks for tackling this @TrMen !

dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
# Description
Previously, only direcly-recursive calls were checked for recursion
depth. But most recursive calls in nushell are mutually recursive since
expressions like `for`, `where`, `try` and `do` all execute a separte
block.

```nushell
def f [] {
    do { f }
}
```
Calling `f` would crash nushell with a stack overflow.

I think the only general way to prevent such a stack overflow is to
enforce a maximum call stack depth instead of only disallowing directly
recursive calls.

This commit also moves that logic into `eval_call()` instead of
`eval_block()` because the recursion limit is tracked in the `Stack`,
but not all blocks are evaluated in a new stack. Incrementing the
recursion depth of the caller's stack would permanently increment that
for all future calls.

Fixes nushell#11667

# User-Facing Changes
Any function call can now fail with `recursion_limit_reached` instead of
just directly recursive calls. Mutually-recursive calls no longer crash
nushell.

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nushell crush on function recursion

4 participants