Skip to content

Commit

Permalink
use ThreadStateGuard to avoid deadlocks acquiring the dtype cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ngoldbaum committed Feb 19, 2025
1 parent 5f4c90a commit 4ee024a
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use crate::dtype::{clone_methods_impl, Element, PyArrayDescr, PyArrayDescrMethod
use crate::npyffi::{
PyArray_DatetimeDTypeMetaData, PyDataType_C_METADATA, NPY_DATETIMEUNIT, NPY_TYPES,
};
use crate::ThreadStateGuard;

/// Represents the [datetime units][datetime-units] supported by NumPy
///
Expand Down Expand Up @@ -223,9 +224,14 @@ impl TypeDescriptors {

#[allow(clippy::wrong_self_convention)]
fn from_unit<'py>(&self, py: Python<'py>, unit: NPY_DATETIMEUNIT) -> Bound<'py, PyArrayDescr> {
// FIXME probably a deadlock risk here due to the GIL? Might need MutexExt trait in PyO3
// Detach from the runtime to avoid deadlocking on acquiring the mutex.
let ts_guard = ThreadStateGuard::new();

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

// Now we hold the mutex so it's safe to re-attach to the runtime.
drop(ts_guard);

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

0 comments on commit 4ee024a

Please sign in to comment.