Skip to content

Commit

Permalink
Add deadlock-avoidance using direct FFI calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Feb 13, 2025
1 parent 177927c commit cc109bf
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,24 @@ impl TypeDescriptors {
byteorder: c_char,
size: usize,
) -> Bound<'py, PyArrayDescr> {
// FIXME probably a deadlock risk here due to the GIL? Might need MutexExt trait in PyO3
// FIXME create a proper MutexExt trait that handles poisoning and upstream to PyO3
struct Guard(*mut pyo3::ffi::PyThreadState);

impl Drop for Guard {
fn drop(&mut self) {
unsafe { pyo3::ffi::PyEval_RestoreThread(self.0) };
}
}

// we are currently attached to the python runtime and we might block trying to acquire
// the dtype cache mutex, so detach to avoid a possible deadlock.
let ts_guard = Guard(unsafe { pyo3::ffi::PyEval_SaveThread() });

let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");

// we've acquire the dtype cache lock so it's safe to re-attach to the runtime
drop(ts_guard);

let dtype = match dtypes.get_or_insert_with(Default::default).entry(size) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
Expand Down

0 comments on commit cc109bf

Please sign in to comment.