Repository navigation
[parser] implement optional depth limits - #29181
samuelcolvin wants to merge 2 commits into
Conversation
| } | ||
|
|
||
| /// The placeholder returned when an expression is nested too deeply to parse. | ||
| fn recursion_recovery_parsed_expr(&self) -> ParsedExpr { |
There was a problem hiding this comment.
this seems to removed have the performance penalty that other solutions introduced.
Memory usage reportMemory usage unchanged ✅ |
|
|
|
@MichaReiser I would love to hear what you think? Is this good, or directionally correct, or would you prefer another approach to limiting depth. |
|
Micha is out for another week. I'm sure he'll take a look when he's back! |
|
any update here? |
|
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? |
|
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:
That's why I'd prefer a off-by-default stacker feature that monty doesn't enable (but ty and Ruff can) |
|
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. |
|
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) |
|
Great! Thank you. |
|
I'll close this in favor of #29505 Thanks for starting the discussion and sorry for the long wait. |
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.