Skip to content

Commit 4ee024a

Browse files
committed
use ThreadStateGuard to avoid deadlocks acquiring the dtype cache
1 parent 5f4c90a commit 4ee024a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/datetime.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ use crate::dtype::{clone_methods_impl, Element, PyArrayDescr, PyArrayDescrMethod
6767
use crate::npyffi::{
6868
PyArray_DatetimeDTypeMetaData, PyDataType_C_METADATA, NPY_DATETIMEUNIT, NPY_TYPES,
6969
};
70+
use crate::ThreadStateGuard;
7071

7172
/// Represents the [datetime units][datetime-units] supported by NumPy
7273
///
@@ -223,9 +224,14 @@ impl TypeDescriptors {
223224

224225
#[allow(clippy::wrong_self_convention)]
225226
fn from_unit<'py>(&self, py: Python<'py>, unit: NPY_DATETIMEUNIT) -> Bound<'py, PyArrayDescr> {
226-
// FIXME probably a deadlock risk here due to the GIL? Might need MutexExt trait in PyO3
227+
// Detach from the runtime to avoid deadlocking on acquiring the mutex.
228+
let ts_guard = ThreadStateGuard::new();
229+
227230
let mut dtypes = self.dtypes.lock().expect("dtype cache poisoned");
228231

232+
// Now we hold the mutex so it's safe to re-attach to the runtime.
233+
drop(ts_guard);
234+
229235
let dtype = match dtypes.get_or_insert_with(Default::default).entry(unit) {
230236
Entry::Occupied(entry) => entry.into_mut(),
231237
Entry::Vacant(entry) => {

0 commit comments

Comments
 (0)