René's URL Explorer Experiment


Title: Fix segfault on cyclic or deeply-nested AST in `compile()` by changjoon-park · Pull Request #7630 · RustPython/RustPython · GitHub

Open Graph Title: Fix segfault on cyclic or deeply-nested AST in `compile()` by changjoon-park · Pull Request #7630 · RustPython/RustPython

X Title: Fix segfault on cyclic or deeply-nested AST in `compile()` by changjoon-park · Pull Request #7630 · RustPython/RustPython

Description: Closes #4862. Symptom compile() with a cyclic or pathologically deep AST object (built via the ast module) overflows the native stack and crashes RustPython with SIGSEGV. CPython raises RecursionError: maximum recursion depth exceeded while traversing node. Reproduction import ast d = ast.UnaryOp(op=ast.Not(), lineno=0, col_offset=0) d.operand = d # self-reference -> infinite traversal compile(ast.Expression(d), '', 'eval') Before After RustPython SIGSEGV RecursionError: maximum recursion depth exceeded while traversing AST node CPython 3.14 RecursionError: maximum recursion depth exceeded while traversing 'expr' node (unchanged) Root cause The ast_from_object conversion from a Python ast object tree to the internal ruff_python_ast types in crates/vm/src/stdlib/_ast/node.rs recursed unconditionally: Box::ast_from_object called T::ast_from_object once per descent (no guard) Vec::ast_from_object called Node::ast_from_object per element (no guard) A cycle in the Python-level AST turns either call into unbounded recursion, which overflows the Rust native stack. Rust cannot recover from a stack overflow — the process is killed. Fix Wrap both recursive descents in vm.with_recursion(...), which is the existing machinery RustPython uses elsewhere to translate native recursion into a Python-level RecursionError at the configured limit (default 1000). No new infrastructure, no configuration changes. // Box::ast_from_object — single recursive descent vm.with_recursion("while traversing AST node", || { T::ast_from_object(vm, source_file, object).map(Self::new) }) // Vec::ast_from_object — per-element descent vm.extract_elements_with(&object, |obj| { vm.with_recursion("while traversing AST node", || { Node::ast_from_object(vm, source_file, obj) }) }) The "while traversing AST node" label matches CPython's RecursionError message style for this failure mode. Why both Box and Vec must be guarded All recursive AST fields use one of these two containers: Box etc. — direct single-child descent (most binary/unary expressions, attribute access, subscripts, etc.) Vec etc. — multi-child descent (list/tuple/set literals, dict values, match patterns, call args, etc.) An earlier iteration of this patch only guarded Box. Testing showed that List.elts = [self] and Tuple.elts = [self] still crashed because they traverse via Vec. Guarding both closes every cycle path I could construct. Verification Direct-cycle cases (were segfaulting; now raise RecursionError) UnaryOp.operand = self BinOp.left = self Call.func = self Attribute.value = self Subscript.value = self IfExp.test = self List.elts = [self] Tuple.elts = [self] Set.elts = [self] Dict.values = [self] ListComp.elt = self Indirect cycles A.operand = B; B.operand = A — caught on the same counter. Deep non-cyclic input Depth Behavior 500 compiles OK 900 compiles OK 999 compiles OK 1500 / 3000 / 5000 RecursionError (safe; no crash) Regression $ ./target/release/rustpython -m test test_ast test_compile All 2 tests OK. Result: SUCCESS A new regression test in extra_tests/snippets/stdlib_ast.py exercises 6 representative cycle patterns across both Box and Vec descent paths; it passes on both RustPython (with this patch) and CPython 3.14. Scope Targeted at the ast_from_object path, which is the only way a cyclic tree can enter RustPython's compilation pipeline (parsed source cannot be cyclic; ruff parser produces trees, not DAGs). ast_to_object (reverse direction) is only called on trees produced by ruff_python_parser, which does not emit cycles; no user-controlled path reaches it. validate_mod runs after ast_from_object. Since ast_from_object now caps depth via the VM's recursion counter, any AST reaching validate_mod is already depth-bounded; local testing at depth 999 confirms validate_mod handles it without crashing. Why this approach (vs. a dedicated counter in codegen) The initial attempt placed a depth counter inside Compiler::compile_expression (crates/codegen/src/compile.rs). That turned out to be downstream of the crash site — the process is already dead before reaching codegen. The fix lives where the crash happens: the ast_from_object traversal. Using the VM's existing with_recursion keeps the mechanism consistent with how RustPython already converts native overflows to RecursionError elsewhere (e.g. during __repr__ recursion, __eq__ recursion). Summary by CodeRabbit Bug Fixes Improved handling of cyclic and pathologically deep AST structures to prevent stack overflow and raise proper recursion errors instead. Tests Added comprehensive regression tests for cyclic AST nodes to ensure proper error handling across various node types.

Open Graph Description: Closes #4862. Symptom compile() with a cyclic or pathologically deep AST object (built via the ast module) overflows the native stack and crashes RustPython with SIGSEGV. CPython raises RecursionEr...

X Description: Closes #4862. Symptom compile() with a cyclic or pathologically deep AST object (built via the ast module) overflows the native stack and crashes RustPython with SIGSEGV. CPython raises RecursionEr...

Opengraph URL: https://github.com/RustPython/RustPython/pull/7630

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:5432d099-953e-afc6-22c4-ad079b06ff86
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idE952:28A483:111B137:16AE6FA:6B01E3FA
html-safe-nonce2d92c0d5d807e40a4e0e5c160aa450fb522b3dd917ea8bdc88525a22f730a3a1
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOTUyOjI4QTQ4MzoxMTFCMTM3OjE2QUU2RkE6NkIwMUUzRkEiLCJ2aXNpdG9yX2lkIjoiNzQxMzkxNDkzMTE2NDIxMDE3MSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac8a6603f446543c0c2dd459a37c022b1c878403f2e5a6448e91be184b93cc0b09
hovercard-subject-tagpull_request:3551612941
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/RustPython/RustPython/pull/7630/files
twitter:imagehttps://avatars.githubusercontent.com/u/131537123?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/131537123?s=400&v=4
og:image:altCloses #4862. Symptom compile() with a cyclic or pathologically deep AST object (built via the ast module) overflows the native stack and crashes RustPython with SIGSEGV. CPython raises RecursionEr...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Nonecc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git
octolytics-dimension-user_id39710557
octolytics-dimension-user_loginRustPython
octolytics-dimension-repository_id135201145
octolytics-dimension-repository_nwoRustPython/RustPython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id135201145
octolytics-dimension-repository_network_root_nwoRustPython/RustPython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release8c77c9e8439031ad54c75a250694842892f693e1
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/RustPython/RustPython/pull/7630/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fpull%2F7630%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FRustPython%2FRustPython%2Fpull%2F7630%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=RustPython%2FRustPython
Reloadhttps://github.com/RustPython/RustPython/pull/7630/files
Reloadhttps://github.com/RustPython/RustPython/pull/7630/files
Reloadhttps://github.com/RustPython/RustPython/pull/7630/files
RustPythonhttps://github.com/RustPython
RustPythonhttps://github.com/RustPython/RustPython
Codehttps://github.com/RustPython/RustPython
Issues195 (195)https://github.com/RustPython/RustPython/issues
Pull requests133 (133)https://github.com/RustPython/RustPython/pulls
Discussionshttps://github.com/RustPython/RustPython/discussions
Actionshttps://github.com/RustPython/RustPython/actions
Projectshttps://github.com/RustPython/RustPython/projects
Wikihttps://github.com/RustPython/RustPython/wiki
Security and qualityhttps://github.com/RustPython/RustPython/security
Insightshttps://github.com/RustPython/RustPython/insights
Sign up for GitHub https://github.com/signup?return_to=%2FRustPython%2FRustPython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2FRustPython%2FRustPython%2Fissues%2Fnew%2Fchoose
youknowonehttps://github.com/youknowone
RustPython:mainhttps://github.com/RustPython/RustPython/tree/main
changjoon-park:fix-ast-cycle-segfaulthttps://github.com/changjoon-park/RustPython/tree/fix-ast-cycle-segfault
Conversation 3 https://github.com/RustPython/RustPython/pull/7630
Commits 1 https://github.com/RustPython/RustPython/pull/7630/commits
Checks 19 https://github.com/RustPython/RustPython/pull/7630/checks
Files changed https://github.com/RustPython/RustPython/pull/7630/files
Please reload this pagehttps://github.com/RustPython/RustPython/pull/7630/files
Fix segfault on cyclic or deeply-nested AST in compile() https://github.com/RustPython/RustPython/pull/7630/files#top
Show all changes 1 commit https://github.com/RustPython/RustPython/pull/7630/files
03ca2d5 Fix segfault on cyclic or deeply-nested AST in compile() changjoon-park Apr 19, 2026 https://github.com/RustPython/RustPython/pull/7630/commits/03ca2d5005c17a1eb658e36bd91c3c5a0710aebd
Clear filters https://github.com/RustPython/RustPython/pull/7630/files
Please reload this pagehttps://github.com/RustPython/RustPython/pull/7630/files
Please reload this pagehttps://github.com/RustPython/RustPython/pull/7630/files
node.rs https://github.com/RustPython/RustPython/pull/7630/files#diff-f019f01b876ba772b7f443a8b1c199720dee18bd4258d5cb50e57e349e3e849e
stdlib_ast.py https://github.com/RustPython/RustPython/pull/7630/files#diff-afc2b772a906bf8bb32979d73b29dd0c53372ea55eecd804e281e7f55c5badb3
crates/vm/src/stdlib/_ast/node.rshttps://github.com/RustPython/RustPython/pull/7630/files#diff-f019f01b876ba772b7f443a8b1c199720dee18bd4258d5cb50e57e349e3e849e
View file https://github.com/changjoon-park/RustPython/blob/03ca2d5005c17a1eb658e36bd91c3c5a0710aebd/crates/vm/src/stdlib/_ast/node.rs
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/RustPython/RustPython/pull/7630/{{ revealButtonHref }}
https://github.com/RustPython/RustPython/pull/7630/files#diff-f019f01b876ba772b7f443a8b1c199720dee18bd4258d5cb50e57e349e3e849e
https://github.com/RustPython/RustPython/pull/7630/files#diff-f019f01b876ba772b7f443a8b1c199720dee18bd4258d5cb50e57e349e3e849e
https://github.com/RustPython/RustPython/pull/7630/files#diff-f019f01b876ba772b7f443a8b1c199720dee18bd4258d5cb50e57e349e3e849e
extra_tests/snippets/stdlib_ast.pyhttps://github.com/RustPython/RustPython/pull/7630/files#diff-afc2b772a906bf8bb32979d73b29dd0c53372ea55eecd804e281e7f55c5badb3
View file https://github.com/changjoon-park/RustPython/blob/03ca2d5005c17a1eb658e36bd91c3c5a0710aebd/extra_tests/snippets/stdlib_ast.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/RustPython/RustPython/pull/7630/{{ revealButtonHref }}
https://github.com/RustPython/RustPython/pull/7630/files#diff-afc2b772a906bf8bb32979d73b29dd0c53372ea55eecd804e281e7f55c5badb3
Please reload this pagehttps://github.com/RustPython/RustPython/pull/7630/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.