Skip to content

$RecursionLimit: default and maximum are 1024, as in WMA - #2128

Merged
tombouctou merged 5 commits into
masterfrom
recursion-limit-1024
Nov 10, 2026
Merged

tombouctou merged 5 commits into
masterfrom
recursion-limit-1024

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

As suggested in #2121: the default $RecursionLimit goes from 200 to 1024, the value in Wolfram Engine 15, and the largest value that can be set goes from 512 to 1024 (MATHICS_MAX_RECURSION_DEPTH still raises it).

A bug this exposed

MathicsSession.reset() gave back the default $RecursionLimit but left Python's recursion limit at whatever the last assignment had set. After $RecursionLimit = 511 and a reset, Mathics3 allowed 1024 levels while Python stopped at about 511, and session.evaluate raised RecursionError. reset() now restores Python's limit too, with a regression test. The bug is also on master; the higher default makes it visible.

Measured

At 1024 on 3.11, 3.12, 3.13 and 3.14 (macOS arm64), on the main thread:

  • Runaway recursions (a = a + a, and a definition through PatternTest, Condition and Module) stop with "Recursion depth of 1024 exceeded." The heavier one takes about 0.2 s to abort, against 0.03 s at 200.
  • Full pytest and doctests on 3.13 (Linux): 4537 passed, doctests OK. Master with the same settings: 4536 passed, so the only difference is the new test.

Trade-offs

  • 3.11 only: Python's limit at startup goes from 8200 to 41160. On a held expression nested 60000 deep (SetAttributes[h, HoldAll]; e = Nest[h, x, 60000]), e === e2 and FreeQ[e, y] now crash 3.11 with a segmentation fault; at 200 they did not. 3.14 is fine on the same cases, and 3.11 is on its way out.
  • Depth on that same expression takes longer before it aborts: 5.5 s against 0.13 s on 3.14.
  • Windows and Pyodide were not tested beyond CI, and their stacks differ from the above.

#2121 still has a comment in settings.py that mentions 512, which should change if this goes in first.

🤖 Generated with Claude Code

tombouctou and others added 2 commits November 9, 2026 05:23
The default was 200 and the largest value that could be set was 512.
Wolfram Engine 15 has $RecursionLimit = 1024.

MathicsSession.reset() now also resets Python's recursion limit. It gave
back the default $RecursionLimit but left Python's limit at whatever the
last assignment had set, so a recursion could stop before $RecursionLimit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rocky

rocky commented Nov 9, 2026

Copy link
Copy Markdown
Member

@tombouctou sorry my merge didn't do the right thing. I can't look at to fix right now though.

@rocky

rocky commented Nov 9, 2026

Copy link
Copy Markdown
Member

@tombouctou, please double-check the merge I made with master, and merge this in when you are satisfied.

Correct some typos and improve comments in settings.py

The main motivation was changing 1024 to 512 in a comment.
@tombouctou
tombouctou merged commit 057d2a0 into master Nov 10, 2026
16 checks passed
@tombouctou
tombouctou deleted the recursion-limit-1024 branch November 10, 2026 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants