-
Notifications
You must be signed in to change notification settings - Fork 856
fix multithreaded access to freelist pyclasses #4902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a4f3533
b2df9f8
84892ef
9c77246
774487d
7645409
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* Fixed thread-unsafe implementation of freelist pyclasses on the free-threaded build. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ use crate::{ | |
exceptions::{PyAttributeError, PyNotImplementedError, PyRuntimeError, PyValueError}, | ||
ffi, | ||
impl_::{ | ||
freelist::FreeList, | ||
freelist::PyObjectFreeList, | ||
pycell::{GetBorrowChecker, PyClassMutability, PyClassObjectLayout}, | ||
pyclass_init::PyObjectInit, | ||
pymethods::{PyGetterDef, PyMethodDefType}, | ||
|
@@ -20,6 +20,7 @@ use std::{ | |
marker::PhantomData, | ||
os::raw::{c_int, c_void}, | ||
ptr::NonNull, | ||
sync::Mutex, | ||
thread, | ||
}; | ||
|
||
|
@@ -912,7 +913,7 @@ use super::{pycell::PyClassObject, pymethods::BoundRef}; | |
/// Do not implement this trait manually. Instead, use `#[pyclass(freelist = N)]` | ||
/// on a Rust struct to implement it. | ||
pub trait PyClassWithFreeList: PyClass { | ||
fn get_free_list(py: Python<'_>) -> &mut FreeList<*mut ffi::PyObject>; | ||
fn get_free_list(py: Python<'_>) -> &'static Mutex<Option<PyObjectFreeList>>; | ||
} | ||
|
||
/// Implementation of tp_alloc for `freelist` classes. | ||
|
@@ -933,7 +934,8 @@ pub unsafe extern "C" fn alloc_with_freelist<T: PyClassWithFreeList>( | |
// If this type is a variable type or the subtype is not equal to this type, we cannot use the | ||
// freelist | ||
if nitems == 0 && subtype == self_type { | ||
if let Some(obj) = T::get_free_list(py).pop() { | ||
let mut free_list = T::get_free_list(py).lock().unwrap(); | ||
if let Some(obj) = free_list.as_mut().unwrap().pop() { | ||
ffi::PyObject_Init(obj, subtype); | ||
ngoldbaum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return obj as _; | ||
} | ||
|
@@ -953,7 +955,10 @@ pub unsafe extern "C" fn free_with_freelist<T: PyClassWithFreeList>(obj: *mut c_ | |
T::type_object_raw(Python::assume_gil_acquired()), | ||
ffi::Py_TYPE(obj) | ||
); | ||
if let Some(obj) = T::get_free_list(Python::assume_gil_acquired()).insert(obj) { | ||
let mut free_list = T::get_free_list(Python::assume_gil_acquired()) | ||
.lock() | ||
.unwrap(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I manually insert a panic on the line below it does indeed crash Python if I modify one of the examples to use a freelist pyclass:
I did the test with this diff:
Are there any examples elsewhere of how we set up panic traps that I might look at? FWIW, any panics in this function aren't new in this PR, so I don't think using The freelist wraps a boxed slice (with your suggestion) and it uses indexing so on debug builds it's possible for an OOB access to generate a panic. I don't immediately see other ways. |
||
if let Some(obj) = free_list.as_mut().unwrap().insert(obj) { | ||
let ty = ffi::Py_TYPE(obj); | ||
|
||
// Deduce appropriate inverse of PyType_GenericAlloc | ||
|
Uh oh!
There was an error while loading. Please reload this page.