From ce18f79d71f4d3eac54f55f7633cf08d2f57b64e Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Mon, 30 Sep 2024 14:19:42 -0600 Subject: [PATCH] Don't use PyList.get_item_unchecked() on free-threaded build (#4539) * disable list.get_item_unchecked() on the free-threaded build * add changelog entry --- newsfragments/4539.removed.md | 3 +++ src/types/list.rs | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 newsfragments/4539.removed.md diff --git a/newsfragments/4539.removed.md b/newsfragments/4539.removed.md new file mode 100644 index 00000000000..dd1da0169b6 --- /dev/null +++ b/newsfragments/4539.removed.md @@ -0,0 +1,3 @@ +* `PyListMethods::get_item_unchecked` is disabled on the free-threaded build. + It relies on accessing list internals without any locking and is not + thread-safe without the GIL to synchronize access. diff --git a/src/types/list.rs b/src/types/list.rs index 4db14849d46..a8952273341 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -182,7 +182,7 @@ pub trait PyListMethods<'py>: crate::sealed::Sealed { /// # Safety /// /// Caller must verify that the index is within the bounds of the list. - #[cfg(not(Py_LIMITED_API))] + #[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))] unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny>; /// Takes the slice `self[low:high]` and returns it as a new list. @@ -305,7 +305,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> { /// # Safety /// /// Caller must verify that the index is within the bounds of the list. - #[cfg(not(Py_LIMITED_API))] + #[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))] unsafe fn get_item_unchecked(&self, index: usize) -> Bound<'py, PyAny> { // PyList_GET_ITEM return borrowed ptr; must make owned for safety (see #890). ffi::PyList_GET_ITEM(self.as_ptr(), index as Py_ssize_t) @@ -496,9 +496,9 @@ impl<'py> BoundListIterator<'py> { } unsafe fn get_item(&self, index: usize) -> Bound<'py, PyAny> { - #[cfg(any(Py_LIMITED_API, PyPy))] + #[cfg(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED))] let item = self.list.get_item(index).expect("list.get failed"); - #[cfg(not(any(Py_LIMITED_API, PyPy)))] + #[cfg(not(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED)))] let item = self.list.get_item_unchecked(index); item } @@ -893,7 +893,7 @@ mod tests { }); } - #[cfg(not(any(Py_LIMITED_API, PyPy)))] + #[cfg(not(any(Py_LIMITED_API, PyPy, Py_GIL_DISABLED)))] #[test] fn test_list_get_item_unchecked_sanity() { Python::with_gil(|py| {