Skip to main content

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