From 0e202dfd192946245e5021b938517aa35145ba9c Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Mon, 24 Mar 2025 09:41:40 -0600 Subject: [PATCH] BUG: avoid deadlocks with C++ shared mutex in dispatch cache --- numpy/_core/src/umath/dispatching.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/numpy/_core/src/umath/dispatching.cpp b/numpy/_core/src/umath/dispatching.cpp index d33899b4257b..ba98a9b5c5d1 100644 --- a/numpy/_core/src/umath/dispatching.cpp +++ b/numpy/_core/src/umath/dispatching.cpp @@ -912,7 +912,9 @@ promote_and_get_info_and_ufuncimpl_with_locking( npy_bool legacy_promotion_is_possible) { std::shared_mutex *mutex = ((std::shared_mutex *)((PyArrayIdentityHash *)ufunc->_dispatch_cache)->mutex); + NPY_BEGIN_ALLOW_THREADS mutex->lock_shared(); + NPY_END_ALLOW_THREADS PyObject *info = PyArrayIdentityHash_GetItem( (PyArrayIdentityHash *)ufunc->_dispatch_cache, (PyObject **)op_dtypes); @@ -926,7 +928,9 @@ promote_and_get_info_and_ufuncimpl_with_locking( // cache miss, need to acquire a write lock and recursively calculate the // correct dispatch resolution + NPY_BEGIN_ALLOW_THREADS mutex->lock(); + NPY_END_ALLOW_THREADS info = promote_and_get_info_and_ufuncimpl(ufunc, ops, signature, op_dtypes, legacy_promotion_is_possible); mutex->unlock();