From 42bf29b6f22f79a6b5332d05b7a95d7100ab0a22 Mon Sep 17 00:00:00 2001 From: Paul Stemmet Date: Sat, 16 Mar 2024 16:10:56 +0000 Subject: [PATCH] src/impl_: add ff unsafe-allow-subinterpreters Allow the subinterpreter safeguards to be disabled, so that applications like Ceph's manager can continue to use pyo3 modules without soft crashing. Enabling this feature should be done with caution, as any storage of Py objects in rust statics can lead to undefined behavior. However, not all consumers of pyo3 use global state, and thus a subset of them (such as python-bcrypt) are safe to use in subinterpreter contexts. References: https://github.com/bazaah/aur-ceph/issues/20 References: https://github.com/PyO3/pyo3/pull/2523 References: https://github.com/pyca/cryptography/issues/9016 References: https://github.com/PyO3/pyo3/discussions/2346#discussioncomment-3246505 References: https://github.com/PyO3/pyo3/discussions/2346#discussioncomment-2911159 References: https://github.com/PyO3/pyo3/issues/3451 Signed-off-by: Paul Stemmet --- Cargo.toml | 3 +++ src/impl_/pymodule.rs | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 364fbac8..54779c5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -112,6 +112,9 @@ py-clone = [] # Optimizes PyObject to Vec conversion and so on. nightly = [] +# Disables the checks for use in subinterpreters. +unsafe-allow-subinterpreters = [] + # Activates all additional features # This is mostly intended for testing purposes - activating *all* of these isn't particularly useful. full = [ diff --git a/src/impl_/pymodule.rs b/src/impl_/pymodule.rs index b05652bc..cb76547c 100644 --- a/src/impl_/pymodule.rs +++ b/src/impl_/pymodule.rs @@ -107,7 +107,7 @@ impl ModuleDef { // that static data is not reused across interpreters. // // PyPy does not have subinterpreters, so no need to check interpreter ID. - #[cfg(not(any(PyPy, GraalPy)))] + #[cfg(not(any(PyPy, GraalPy, feature = "unsafe-allow-subinterpreters")))] { // PyInterpreterState_Get is only available on 3.9 and later, but is missing // from python3.dll for Windows stable API on 3.9