pub unsafe trait Ungil { }
Expand description
Types that are safe to access while the GIL is not held.
§Safety
The type must not carry borrowed Python references or, if it does, not allow access to them if the GIL is not held.
See the module-level documentation for more information.
§Examples
This tracking is currently imprecise as it relies on the Send
auto trait on stable Rust.
For example, an Rc
smart pointer should be usable without the GIL, but we currently prevent that:
ⓘ
use std::rc::Rc;
Python::with_gil(|py| {
let rc = Rc::new(42);
py.allow_threads(|| {
println!("{:?}", rc);
});
});
This also implies that the interplay between with_gil
and allow_threads
is unsound, for example
one can circumvent this protection using the send_wrapper
crate:
use send_wrapper::SendWrapper;
Python::with_gil(|py| {
let string = PyString::new_bound(py, "foo");
let wrapped = SendWrapper::new(string);
py.allow_threads(|| {
let sneaky: &Bound<'_, PyString> = &*wrapped;
println!("{:?}", sneaky);
});
});
Fixing this loophole on stable Rust has significant ergonomic issues, but it is fixed when using
nightly Rust and the nightly
feature, c.f. #2141.