Skip to content

Fix VM's infinite recursion crash with musl libc - #7558

Merged
youknowone merged 2 commits into
RustPython:mainfrom
isbm:isbm-recursion-error-bugfix
Apr 4, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
isbm:isbm-recursion-error-bugfix

Conversation

@isbm

@isbm isbm commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

Hi!
This fixes a musl-only false-positive RecursionError during RustPython frozen-stdlib bootstrap.

I was hitting this while building RustPython-powered part where it must be statically built with musl libc. This is an example:


use rustpython::InterpreterBuilderExt;

fn main() {
    let itp = rustpython::InterpreterBuilder::new()
        .init_stdlib()
        .interpreter();
    itp.run(|_| Ok(()));
}

Built for x86_64-unknown-linux-musl, that happily crashes during frozen stdlib init with:

Traceback (most recent call last):
  File "_frozen_importlib", line 55, in <module>
RecursionError:

This error renders the whole VM incapable to run anything from the frozen library.

The problem comes from native C-stack guard in vm/mod.rs, specifically check_c_stack_overflow() function. I opted to the less invasive fix, which just makes the native C-stack probe a no-op on target_env = "musl" but leaves the regular Python recursion-depth limit in place.

Summary by CodeRabbit

  • Bug Fixes
    • Improved virtual machine compatibility on musl and miri environments by refining C stack checking behavior to avoid spurious stack overflow detections and ensure stable operation across affected platforms.

@coderabbitai

coderabbitai Bot commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 081ed57d-11fd-42c2-92b6-2e150b9c4c94

📥 Commits

Reviewing files that changed from the base of the PR and between f8cd0d0 and 97caee0.

📒 Files selected for processing (1)
  • crates/vm/src/vm/mod.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/vm/src/vm/mod.rs

📝 Walkthrough

Walkthrough

Native C stack checking in the VM was disabled for the musl target in addition to miri. Helpers and constants are conditionally compiled out for those targets, with stub implementations returning neutral values when miri or musl is active.

Changes

Cohort / File(s) Summary
C Stack Checking Conditional Compilation
crates/vm/src/vm/mod.rs
Added musl to existing miri conditional exclusions. Marked c_stack_soft_limit and STACK_MARGIN_BYTES with #[allow(dead_code)] for miri/musl. Excluded get_stack_bounds variants for musl. Made calculate_c_stack_soft_limit return 0 and check_c_stack_overflow return false under miri or musl; active implementations compile only when not(miri) and not(target_env = "musl").

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 I nibble bytes and hop on logs,

Musl and Miri now rest like frogs.
Stubs keep checks soft, the build stays bright,
Hopping safe through compile night. 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix VM's infinite recursion crash with musl libc' directly corresponds to the PR's main objective: fixing a musl-specific RecursionError crash.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/vm/src/vm/mod.rs`:
- Around line 1502-1514: The musl build causes clippy dead_code warnings because
calculate_c_stack_soft_limit, STACK_MARGIN_BYTES and get_stack_bounds become
unreachable; add conditional dead_code allowances for musl by applying cfg_attr
to those symbols (e.g. use #[cfg_attr(any(miri, target_env = "musl"),
allow(dead_code))] on calculate_c_stack_soft_limit, STACK_MARGIN_BYTES, and the
get_stack_bounds function or their containing items) so the allow is active for
musl/miri builds but not for other targets.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 2ad92253-8416-4402-b0d8-0d69eaf571e8

📥 Commits

Reviewing files that changed from the base of the PR and between a09afab and f8cd0d0.

📒 Files selected for processing (1)
  • crates/vm/src/vm/mod.rs

Comment thread crates/vm/src/vm/mod.rs Resolved

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks!

@youknowone
youknowone merged commit 87fc454 into RustPython:main Apr 4, 2026
20 checks passed
youknowone pushed a commit to youknowone/RustPython that referenced this pull request Apr 5, 2026
* Fix VM's infinite recursion crash with musl libc

* Lintfix/cleanup warnings
youknowone pushed a commit that referenced this pull request Sep 16, 2026
* Fix VM's infinite recursion crash with musl libc

* Lintfix/cleanup warnings
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