Fix deadlock in c-api when a thread enters a stop_the_world#8355
Merged
Conversation
Contributor
📝 WalkthroughWalkthroughThe C API adds an atomic main-interpreter pointer, publishes it during initialization, and uses it for initialization checks and interpreter-state retrieval. ChangesMain interpreter pointer
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Py_InitializeEx
participant init_main_interpreter
participant MAIN_INTERP_PTR
participant C_API_queries
Py_InitializeEx->>init_main_interpreter: initialize main interpreter
init_main_interpreter->>MAIN_INTERP_PTR: publish interpreter pointer
C_API_queries->>MAIN_INTERP_PTR: load pointer with Acquire ordering
MAIN_INTERP_PTR-->>C_API_queries: initialization status or interpreter state
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Contributor
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/capi/src/lib.rs`:
- Around line 65-68: Update the MAIN_INTERP_PTR.store call in
init_main_interpreter to use Release ordering instead of Relaxed, preserving the
existing pointer publication while synchronizing it with Acquire readers.
In `@crates/capi/src/pystate.rs`:
- Around line 69-70: Update PyInterpreterState_Get to preserve its non-null
contract by checking the result of MAIN_INTERP_PTR.load(Ordering::Acquire) and
invoking the crate’s existing fatal-error path when it is null; otherwise return
the loaded interpreter pointer unchanged.
🪄 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 Plus
Run ID: 7a8375bc-4e7d-4a73-b2ae-1d2cdd420bfb
📒 Files selected for processing (3)
crates/capi/src/lib.rscrates/capi/src/pylifecycle.rscrates/capi/src/pystate.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Store a lock-free pointer to the main interpreter so
Py_IsInitialized&PyInterpreterState_Get/PyEval_SaveThreaddo not have to lock the main Mutex.Tested that
while true; cargo test --lib; end;does not deadlock anymore.Summary by CodeRabbit