Skip to content

Commit 28454cc

Browse files
Fix deadlock in c-api when a thread enters a stop_the_world (#8355)
1 parent 9062f02 commit 28454cc

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

‎crates/capi/src/lib.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![allow(clippy::missing_safety_doc)]
22

33
use crate::pyerrors::init_exception_statics;
4-
use crate::pylifecycle::MAIN_INTERP;
4+
use crate::pylifecycle::{MAIN_INTERP, MAIN_INTERP_PTR};
5+
use core::sync::atomic::Ordering;
56
pub use rustpython_vm::PyObject;
67
use rustpython_vm::{Context, Interpreter};
78
use std::sync::MutexGuard;
@@ -61,4 +62,8 @@ pub fn init_main_interpreter(interpreter: Interpreter) {
6162
// Safety: Interpreter was not initialized before, so we can safely assume the statics are not used
6263
unsafe { init_exception_statics(&Context::genesis().exceptions) };
6364
*interp = Some(interpreter);
65+
MAIN_INTERP_PTR.store(
66+
interp.as_ref().unwrap() as *const _ as *mut _,
67+
Ordering::Release,
68+
);
6469
}

‎crates/capi/src/pylifecycle.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::pyerrors::init_exception_statics;
33
use crate::pystate::ensure_thread_has_vm_attached;
44
use alloc::ffi::CString;
55
use core::ffi::{c_char, c_int, c_ulong};
6+
use core::sync::atomic::{AtomicPtr, Ordering};
67
use rustpython_vm::common::rc::PyRc;
78
use rustpython_vm::stdlib::sys;
89
use rustpython_vm::version::{MAJOR, MICRO, MINOR, RUSTPYTHON_BUILD_INFO, VERSION_HEX};
@@ -11,6 +12,7 @@ use rustpython_vm::{Context, Interpreter};
1112
use std::sync::{LazyLock, Mutex};
1213

1314
pub(crate) static MAIN_INTERP: Mutex<Option<Interpreter>> = Mutex::new(None);
15+
pub(crate) static MAIN_INTERP_PTR: AtomicPtr<Interpreter> = AtomicPtr::new(core::ptr::null_mut());
1416

1517
/// Request a thread local vm from the main interpreter
1618
pub(crate) fn request_vm_from_interpreter() -> ThreadedVirtualMachine {
@@ -25,7 +27,7 @@ pub static Py_Version: c_ulong = VERSION_HEX as c_ulong;
2527

2628
#[unsafe(no_mangle)]
2729
pub extern "C" fn Py_IsInitialized() -> c_int {
28-
get_main_interpreter().is_some() as c_int
30+
!MAIN_INTERP_PTR.load(Ordering::Acquire).is_null() as c_int
2931
}
3032

3133
#[unsafe(no_mangle)]
@@ -52,6 +54,10 @@ pub extern "C" fn Py_InitializeEx(_initsigs: c_int) {
5254
})
5355
.build()
5456
.into();
57+
MAIN_INTERP_PTR.store(
58+
interp.as_ref().unwrap() as *const _ as *mut _,
59+
Ordering::Release,
60+
);
5561
drop(interp);
5662
ensure_thread_has_vm_attached();
5763
}

‎crates/capi/src/pystate.rs‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::get_main_interpreter;
2-
use crate::pylifecycle::request_vm_from_interpreter;
1+
use crate::pylifecycle::{MAIN_INTERP_PTR, request_vm_from_interpreter};
32
use crate::util::FfiResult;
43
use core::ffi::c_int;
4+
use core::sync::atomic::Ordering;
55
use rustpython_vm::vm::thread::{
66
CurrentVmAttachState, SavedThreadState, attach_current_thread, release_current_thread,
77
restore_current_thread, save_current_thread, with_current_vm,
@@ -67,11 +67,12 @@ pub unsafe extern "C" fn PyEval_RestoreThread(state: *mut PyThreadState) {
6767

6868
#[unsafe(no_mangle)]
6969
pub extern "C" fn PyInterpreterState_Get() -> *mut PyInterpreterState {
70-
get_main_interpreter()
71-
.as_ref()
72-
.map(|interp| interp as *const PyInterpreterState)
73-
.expect("PyInterpreterState_Get called but no main interpreter was found")
74-
.cast_mut()
70+
let ptr = MAIN_INTERP_PTR.load(Ordering::Acquire);
71+
assert!(
72+
!ptr.is_null(),
73+
"PyInterpreterState_Get() called but the interpreter is not initialized"
74+
);
75+
ptr
7576
}
7677

7778
#[unsafe(no_mangle)]

0 commit comments

Comments
 (0)