Skip to content

[parser] implement optional depth limits - #29181

Closed
samuelcolvin wants to merge 2 commits into
astral-sh:mainfrom
samuelcolvin:parser-max-recursion-depth
Closed

samuelcolvin wants to merge 2 commits into
astral-sh:mainfrom
samuelcolvin:parser-max-recursion-depth

Conversation

@samuelcolvin

Copy link
Copy Markdown
Contributor

Summary

Add back depth limits while parsing that were removed by #25464.

#25464 used stacker to avoid stack overflow while parsing deeply nested code, however that introduces a new risk - unlimited memory use. The memory growth can't even be caught by our custom allocator in monty since it uses mmap.

We tried to work around this in Monty with pydantic/monty#906, but it's brittle and introduces a performance penalty by parsing the source twice (3 times if you included type checking).

This could also be implemented as a crates feature to avoid any runtime impact by default, but that seemed unnecessary given there's no noticable performance impact to this change.

Test Plan

This work includes tests.

}

/// The placeholder returned when an expression is nested too deeply to parse.
fn recursion_recovery_parsed_expr(&self) -> ParsedExpr {

@samuelcolvin samuelcolvin Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to removed have the performance penalty that other solutions introduced.

@astral-sh-bot

astral-sh-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre added the parser Related to the parser label Oct 7, 2026
@AlexWaygood AlexWaygood changed the title implement optional depth limits [parser] implement optional depth limits Oct 7, 2026
@samuelcolvin

Copy link
Copy Markdown
Contributor Author

@MichaReiser I would love to hear what you think? Is this good, or directionally correct, or would you prefer another approach to limiting depth.

@ntBre

ntBre commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Micha is out for another week. I'm sure he'll take a look when he's back!

@samuelcolvin

Copy link
Copy Markdown
Contributor Author

any update here?

@MichaReiser

Copy link
Copy Markdown
Member

Hi Samuel. I was out until today.

I assume Monty can handle regular stack overflows. If so, would it be an option if we made stacker an optional feature so that Monty can opt-out?

@MichaReiser

Copy link
Copy Markdown
Member

I think my concern here mainly is that it is a fix for the common cases, but I believe you can still construct a program that would trigger the exact same symptoms:

  • Create a program that exhausts all available memory (but still succeeds to allocate). This may not require recursion.
  • Depending on where the parser is called from, the platform specific stack size etc, trigger a stacker call that allocates

That's why I'd prefer a off-by-default stacker feature that monty doesn't enable (but ty and Ruff can)

@samuelcolvin

Copy link
Copy Markdown
Contributor Author

Hi @MichaReiser yes we can handle regular stack overflows, so a feature to disable stacker would work great.

For context: we run the interpreter (and parser) in a subprocess, so stack overflows result in justa crash message. We can also handle large memory use via the allocator since we have a custom allocator that counts allocations and errors when the limit is exceeded.

The one (as far as I know) gap in the armour is mmap which we can't really protect against - hence stacker being a problem.

@MichaReiser

Copy link
Copy Markdown
Member

Perfect. Let's make staker an off-by-default feature then and we'll only enable it in our CLIs, playgrounds (entry binaries). This way, it should remain off for you (since, as far as I understand, you don't link against our cli)

@samuelcolvin

Copy link
Copy Markdown
Contributor Author

Great! Thank you.

@MichaReiser

Copy link
Copy Markdown
Member

I'll close this in favor of #29505 Thanks for starting the discussion and sorry for the long wait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants