Skip to content

Commit 7dd9d42

Browse files
committed
Be more explicit of the soundness hole implied by tying Ungil to Send and mention the available solution.
1 parent ae99739 commit 7dd9d42

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/marker.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ use std::os::raw::c_int;
148148
///
149149
/// ```compile_fail
150150
/// # use pyo3::prelude::*;
151+
/// use std::rc::Rc;
152+
///
151153
/// Python::with_gil(|py| {
152154
/// let rc = Rc::new(42);
153155
///
@@ -157,7 +159,8 @@ use std::os::raw::c_int;
157159
/// });
158160
/// ```
159161
///
160-
/// This also implies that one can circumvent this protection using e.g. the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
162+
/// This also implies that the interplay between `with_gil` and `allow_threads` is unsound, for example
163+
/// one can circumvent this protection using the [`send_wrapper`](https://docs.rs/send_wrapper/) crate:
161164
///
162165
/// ```no_run
163166
/// # use pyo3::prelude::*;
@@ -176,6 +179,9 @@ use std::os::raw::c_int;
176179
/// });
177180
/// });
178181
/// ```
182+
///
183+
/// Fixing this loophole on stable Rust has significant ergonomic issues, but it is fixed when using
184+
/// nightly Rust and the `nightly` feature, c.f. [#2141](https://github.com/PyO3/pyo3/issues/2141).
179185
#[cfg_attr(docsrs, doc(cfg(all())))] // Hide the cfg flag
180186
#[cfg(not(feature = "nightly"))]
181187
pub unsafe trait Ungil {}
@@ -240,6 +246,22 @@ unsafe impl<T: Send> Ungil for T {}
240246
/// });
241247
/// });
242248
/// ```
249+
///
250+
/// This also enables using non-[`Send`] types in `allow_threads`,
251+
/// at least if they are not also bound to the GIL:
252+
///
253+
/// ```rust
254+
/// # use pyo3::prelude::*;
255+
/// use std::rc::Rc;
256+
///
257+
/// Python::with_gil(|py| {
258+
/// let rc = Rc::new(42);
259+
///
260+
/// py.allow_threads(|| {
261+
/// println!("{:?}", rc);
262+
/// });
263+
/// });
264+
/// ```
243265
#[cfg(feature = "nightly")]
244266
pub unsafe auto trait Ungil {}
245267

0 commit comments

Comments
 (0)