Skip to content

Handle parser stack overflows more gracefully #22930

Closed

Description

@MeGaGiGaGon

Summary

Currently, if you have a file that causes a stack overflow in the parser ruff/ty will crash with the usual message thread 'main' (...) has overflowed its stack. It would be nice if there was a better/more clear error when that happens, to differentiate logic-based errors in ruff/ty from "your file is too big/complex" errors.

Version

ruff 0.14.14 (8b2e7b3 2026-01-22) / ty 0.0.14 (16597f5e2 2026-01-26)

Activity

  1. added
    parser
    on Jan 28, 2026
  2. MichaReiser commented on Mar 2, 2026

    @MichaReiser
    Member

    I think we should fix the parser instead. It shouldn't panic for any <= 4GB Python file.

    This may require rewriting some recursive parsing to use loops or we can dynamically grow the stack size, using something like stacker

  3. MeGaGiGaGon commented on Mar 2, 2026

    @MeGaGiGaGon
    ContributorAuthor

    I didn't suggest that initially since because in all the things I tried cpython's limits are set lower that where ruff/ty currently stack overflow, so all valid python should also still be covered. It would be cool though.

  4. samuelcolvin commented on Apr 20, 2026

    @samuelcolvin
    Contributor

    It's pretty easy to get a stack oveflow which is unfortunate for those of us using ruff to parase untrusted code.

    Here's a simple MRE that works with main when run with cargo run --release -- 5000:

    //! MRE: pure ruff_python_parser stack overflow on deeply nested parens.
    
    use ruff_python_parser::parse_module;
    
    fn main() {
        let depth: usize = std::env::args()
            .nth(1)
            .and_then(|s| s.parse().ok())
            .unwrap_or(2000);
    
        let mut code = String::with_capacity(depth * 2 + 1);
        for _ in 0..depth {
            code.push('(');
        }
        code.push('1');
        for _ in 0..depth {
            code.push(')');
        }
    
        println!("parsing expr with paren depth = {depth}, bytes = {}", code.len());
        match parse_module(&code) {
            Ok(_) => println!("parsed OK"),
            Err(e) => println!("parse error: {e}"),
        }
        println!("survived");
    }
  5. added a commit that references this issue on Apr 30, 2026
    2462b87
  6. added 4 commits that reference this issue on May 19, 2026
    ad97089
    6ea4ba8
    4a0551e
    c423054
  7. MichaReiser commented on Aug 14, 2026

    @MichaReiser
    Member

    I think this should now be in a better spot with #25464

    This does not protect against downstream tools panicking in a visitor, a function recursively traversing an AST, or even Drop, Eq, Hash etc exceeding the available stack size. If there are cases where you run into this with Ruff or ty, please open an issue with an MRE.

    If you run into cases where the parser still overflows, please open an issue with an MRE where we can discuss whether this is reasonable to protect against in Ruff.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    parser

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions