Skip to main content

pyforge_ffi/
pystate.rs

1use crate::moduleobject::PyModuleDef;
2use crate::object::PyObject;
3use crate::pytypedefs::{PyInterpreterState, PyThreadState};
4use std::ffi::c_int;
5
6#[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
7use crate::PyFrameObject;
8
9use std::ffi::c_long;
10
11pub const MAX_CO_EXTRA_USERS: c_int = 255;
12
13extern_libpython! {
14    pub fn PyInterpreterState_New() -> *mut PyInterpreterState;
15    pub fn PyInterpreterState_Clear(arg1: *mut PyInterpreterState);
16    pub fn PyInterpreterState_Delete(arg1: *mut PyInterpreterState);
17
18    #[cfg(Py_3_9)]
19    pub fn PyInterpreterState_Get() -> *mut PyInterpreterState;
20
21    pub fn PyInterpreterState_GetDict(arg1: *mut PyInterpreterState) -> *mut PyObject;
22
23    pub fn PyInterpreterState_GetID(arg1: *mut PyInterpreterState) -> i64;
24
25    pub fn PyState_AddModule(arg1: *mut PyObject, arg2: *mut PyModuleDef) -> c_int;
26    pub fn PyState_RemoveModule(arg1: *mut PyModuleDef) -> c_int;
27
28    pub fn PyState_FindModule(arg1: *mut PyModuleDef) -> *mut PyObject;
29
30    pub fn PyThreadState_New(arg1: *mut PyInterpreterState) -> *mut PyThreadState;
31    pub fn PyThreadState_Clear(arg1: *mut PyThreadState);
32    pub fn PyThreadState_Delete(arg1: *mut PyThreadState);
33
34    pub fn PyThreadState_Get() -> *mut PyThreadState;
35}
36
37#[inline]
38pub unsafe fn PyThreadState_GET() -> *mut PyThreadState {
39    PyThreadState_Get()
40}
41
42extern_libpython! {
43    pub fn PyThreadState_Swap(arg1: *mut PyThreadState) -> *mut PyThreadState;
44    pub fn PyThreadState_GetDict() -> *mut PyObject;
45    pub fn PyThreadState_SetAsyncExc(arg1: c_long, arg2: *mut PyObject) -> c_int;
46
47    #[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
48    pub fn PyThreadState_GetInterpreter(arg1: *mut PyThreadState) -> *mut PyInterpreterState;
49    #[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
50    pub fn PyThreadState_GetFrame(arg1: *mut PyThreadState) -> *mut PyFrameObject;
51    #[cfg(any(all(Py_3_9, not(Py_LIMITED_API)), Py_3_10))]
52    pub fn PyThreadState_GetID(arg1: *mut PyThreadState) -> i64;
53}
54
55#[repr(C)]
56#[derive(Copy, Clone, Debug, PartialEq, Eq)]
57pub enum PyGILState_STATE {
58    PyGILState_LOCKED,
59    PyGILState_UNLOCKED,
60}
61
62#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
63struct HangThread;
64
65#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
66impl Drop for HangThread {
67    fn drop(&mut self) {
68        loop {
69            std::thread::park(); // Block forever.
70        }
71    }
72}
73
74// The PyGILState_Ensure function will call pthread_exit during interpreter shutdown,
75// which causes undefined behavior. Redirect to the "safe" version that hangs instead,
76// as Python 3.14 does.
77//
78// See https://github.com/rust-lang/rust/issues/135929
79
80// C-unwind only supported (and necessary) since 1.71. Python 3.14+ does not do
81// pthread_exit from PyGILState_Ensure (https://github.com/python/cpython/issues/87135).
82mod raw {
83    #[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
84    extern_libpython! { "C-unwind" {
85        pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
86    }}
87
88    #[cfg(any(Py_3_14, target_arch = "wasm32"))]
89    extern_libpython! {
90        pub fn PyGILState_Ensure() -> super::PyGILState_STATE;
91    }
92}
93
94#[cfg(not(any(Py_3_14, target_arch = "wasm32")))]
95pub unsafe extern "C" fn PyGILState_Ensure() -> PyGILState_STATE {
96    let guard = HangThread;
97    // If `PyGILState_Ensure` calls `pthread_exit`, which it does on Python < 3.14
98    // when the interpreter is shutting down, this will cause a forced unwind.
99    // doing a forced unwind through a function with a Rust destructor is unspecified
100    // behavior.
101    //
102    // However, currently it runs the destructor, which will cause the thread to
103    // hang as it should.
104    //
105    // And if we don't catch the unwinding here, then one of our callers probably has a destructor,
106    // so it's unspecified behavior anyway, and on many configurations causes the process to abort.
107    //
108    // The alternative is for pyo3 to contain custom C or C++ code that catches the `pthread_exit`,
109    // but that's also annoying from a portability point of view.
110    //
111    // On Windows, `PyGILState_Ensure` calls `_endthreadex` instead, which AFAICT can't be caught
112    // and therefore will cause unsafety if there are pinned objects on the stack. AFAICT there's
113    // nothing we can do it other than waiting for Python 3.14 or not using Windows. At least,
114    // if there is nothing pinned on the stack, it won't cause the process to crash.
115    let ret: PyGILState_STATE = raw::PyGILState_Ensure();
116    std::mem::forget(guard);
117    ret
118}
119
120#[cfg(any(Py_3_14, target_arch = "wasm32"))]
121pub use self::raw::PyGILState_Ensure;
122
123extern_libpython! {
124    pub fn PyGILState_Release(arg1: PyGILState_STATE);
125    pub fn PyGILState_GetThisThreadState() -> *mut PyThreadState;
126}