Skip to content

Add import_ensure_initialized to import_module_level#7076

Merged
youknowone merged 1 commit intoRustPython:mainfrom
youknowone:import
Feb 11, 2026
Merged

Add import_ensure_initialized to import_module_level#7076
youknowone merged 1 commit intoRustPython:mainfrom
youknowone:import

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 11, 2026

Wait for module initialization to complete when a module is already in sys.modules but still being initialized by another thread. Check spec._initializing and call
_lock_unlock_module to block until the import finishes.

Summary by CodeRabbit

  • Bug Fixes
    • Improved module import synchronization to properly handle concurrent and nested imports, ensuring modules are fully initialized before use and preventing edge case issues.

Wait for module initialization to complete when a module is
already in sys.modules but still being initialized by another
thread. Check __spec__._initializing and call
_lock_unlock_module to block until the import finishes.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 11, 2026

📝 Walkthrough

Walkthrough

A new private helper function import_ensure_initialized is added to handle synchronization of module imports. It checks whether a module's __spec__._initializing flag is set and calls importlib's _lock_unlock_module to wait for initialization completion. The helper is integrated into import_module_level to ensure pre-existing modules are fully initialized before returning.

Changes

Cohort / File(s) Summary
Module Import Synchronization
crates/vm/src/import.rs
Added private import_ensure_initialized() helper function that waits for partially-initialized modules to complete their initialization by checking __spec__._initializing and calling importlib's locking mechanism. Integrated into import_module_level to synchronize module lookup and return.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A module waits, caught mid-creation,
But now we ensure initialization—
With locks and flags in harmony,
Imports flow safe and clean, you see! ✨

🚥 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 accurately and concisely summarizes the main change: adding the import_ensure_initialized function to import_module_level.
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 docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉


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.

@youknowone youknowone marked this pull request as ready for review February 11, 2026 00:33
@youknowone youknowone merged commit 36ff461 into RustPython:main Feb 11, 2026
13 checks passed
@youknowone youknowone deleted the import branch February 11, 2026 01:57
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.

1 participant