Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemError: excessive stack use when compiling a large function #94329

Closed
oscarbenjamin opened this issue Jun 27, 2022 · 3 comments
Closed

SystemError: excessive stack use when compiling a large function #94329

oscarbenjamin opened this issue Jun 27, 2022 · 3 comments
Assignees
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@oscarbenjamin
Copy link
Contributor

Bug report

This comes from a recently discovered SymPy issue: sympy/sympy#23690

The following code fails in CPython 3.10 or newer for large N apparently due to the changes in #25403 (CC @markshannon):

#!/usr/bin/env python3

template = """\
def %(name)s(A, B):
    (%(A)s,) = A
    (%(B)s,) = B
    return (%(AB)s,)
"""

def make_function(N):
    name = "monomial_mul"
    A = [f"a{i}" for i in range(N)]
    B = [f"b{i}" for i in range(N)]
    AB = [ "%s + %s" % (a, b) for a, b in zip(A, B) ]
    code = template % dict(name=name, A=", ".join(A), B=", ".join(B), AB=", ".join(AB))
    ns = {}
    exec(code, ns)
    return ns["monomial_mul"]

if __name__ == "__main__":
    from sys import argv
    make_function(int(argv[1]))

In CPython 3.9 or older this can run fine albeit slow for large values of N (I tested 100000).

With CPython 3.10 or current main (3.11b3) this will fail with SystemError for any N greater than 3000:

$ ./python test.py 3000
$ ./python test.py 3001
Traceback (most recent call last):
  File "/home/oscar/current/sympy/cpython/test.py", line 22, in <module>
    make_function(int(argv[1]))
  File "/home/oscar/current/sympy/cpython/test.py", line 17, in make_function
    exec(code, ns)
SystemError: excessive stack use: stack is 3001 deep

I wasn't sure that this was a deliberate effect of the changes or if it should definitely be considered a bug but the comment added in #25403 indicates that it should be:

cpython/Python/compile.c

Lines 49 to 71 in c0453a4

/* A soft limit for stack use, to avoid excessive
* memory use for large constants, etc.
*
* The value 30 is plucked out of thin air.
* Code that could use more stack than this is
* rare, so the exact value is unimportant.
*/
#define STACK_USE_GUIDELINE 30
/* If we exceed this limit, it should
* be considered a compiler bug.
* Currently it should be impossible
* to exceed STACK_USE_GUIDELINE * 100,
* as 100 is the maximum parse depth.
* For performance reasons we will
* want to reduce this to a
* few hundred in the future.
*
* NOTE: Whatever MAX_ALLOWED_STACK_USE is
* set to, it should never restrict what Python
* we can write, just how we compile it.
*/
#define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100)

Your environment

Tested on Ubuntu with a range of Python versions.

@markshannon
Copy link
Member

I wasn't sure that this was a deliberate effect of the changes or if it should definitely be considered a bug

This is a bug. Thanks for the report.

@kumaraditya303 kumaraditya303 added 3.11 only security fixes 3.10 only security fixes 3.12 bugs and security fixes labels Jun 27, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 30, 2022
markshannon added a commit to faster-cpython/cpython that referenced this issue Jun 30, 2022
markshannon pushed a commit that referenced this issue Jun 30, 2022
@DanielNoord
Copy link
Contributor

@markshannon This seems to have been back ported to all relevant versions in #94448, #94446 and #94421

I think it can be closed!

@oscarbenjamin
Copy link
Contributor Author

I guess since this is immediately fixed on relevant branches and #94445 is the continuation issue to look for a better long term fix then this can be closed.

mivanit added a commit to understanding-search/maze-dataset that referenced this issue Jun 12, 2024
```
$ git log --oneline
26db9f6 (HEAD -> tokenizer-overhaul-step2) `test_edge_subsets` passing
64dd2ff wip `test_edge_subsets`
973df5a Made method skeleton for `EdgeGrouping` and `AdjListTokenizer.to_tokens`.
c4253ec `test_edge_subsets` passing
00bd0a7 `test_manhattan_distance` passing
9a52cc7 Updated poetry.lock
f77f077 Bug in CPython raises exception when building `constants._VOCAB_BASE` since it's a large dataclass. python/cpython#94329. Raise Python version requirement to require a version with this bugfix.
fa58ef5 Updated .gitignore for pyenv environment file
ba6404a (origin/tokenizer-overhaul-step2) `AllLatticeEdges.get_edges` written, not tested
```
mivanit pushed a commit to understanding-search/maze-dataset that referenced this issue Jun 12, 2024
… since it's a large dataclass. python/cpython#94329. Raise Python version requirement to require a version with this bugfix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants