Sitelet https://github.com/python/cpython/issues/93671
Skip to content

Python3.10.5 Pattern Matching Excessive Memory Consumption #93671

Description

@tom-p-reichel

Crash report

While writing some code to match a particularly complicated AST, I accidentally wrote a function whose definition causes the Python 3.10.5 interpreter to rapidly consume memory. On my system, it consumed approximately 24 GBs of memory before being killed.

The function is as follows:

def defines(self):
        match self.ast:
                case [S('CoqAst'), [[S('v'), [_, _, [S('expr'), [S('VernacDefinition'), [S('NoDischarge'), S('Definition')], [[[S('v'), [S('Name'), [S('Id'), S(name)]]], _], []], [S('DefineBody'), [], [], [_, _], []]]]]], _]]
                | [S('CoqAst'), [[S('v'), [[S('control'), []], [S('attrs'), []], [S('expr'), [S('VernacStartTheoremProof'), S('Theorem'), [[[[[S('v'), [S('Id'), name]], _], []], _]]]]]], _]]:
                        return name

To replicate, paste the above function into a python file and run it.

Error messages

OOM killing produced no output, save for Killed.

Your environment

  • CPython versions tested on: 3.10.5
  • Operating system and architecture: Tested on both Windows 10 and ArchLinux.

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Jun 10, 2022
  2. Fidget-Spinner commented on Jun 10, 2022

    @Fidget-Spinner
    Member

    Wow. I can reproduce this on 3.11 and 3.12(main) too.

  3. kumaraditya303 commented on Jun 10, 2022

    @kumaraditya303
    Contributor

    This seems like a parser issue.

    Linux perf output:

    Samples: 36K of event 'cpu-clock:pppH', Event count (approx.): 9100000000
    Overhead  Command  Shared Object       Symbol
       7.30%  python   python              [.] _PyPegen_expect_token
       6.68%  python   [kernel.kallsyms]   [k] clear_page_erms
       6.28%  python   [kernel.kallsyms]   [k] __do_page_fault
       4.64%  python   python              [.] closed_pattern_rule
       4.09%  python   python              [.] _PyArena_Malloc
       3.36%  python   python              [.] signed_real_number_rule
       3.09%  python   libc-2.31.so        [.] _int_malloc
       3.05%  python   python              [.] or_pattern_rule
       2.84%  python   python              [.] unicodekeys_lookup_unicode
  4. dignissimus commented on Jun 10, 2022

    @dignissimus
    Contributor

    It might be the related to the depth of the statement, the following also produces a crash

    match p:
        case [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]:
            pass
  5. pablogsal commented on Jun 10, 2022

    @pablogsal
    Member

    Thanks, @tom-p-reichel and @dignissimus for the report and thanks a lot @kumaraditya303 for the initial investigation. Great work!

  6. added a commit that references this issue on Jun 10, 2022
  7. added 2 commits that reference this issue on Jun 10, 2022
  8. added 2 commits that reference this issue on Jun 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.10 (EOL)end of life3.11only security fixes3.12only security fixestype-crashA hard crash of the interpreter, possibly with a core dump

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions