Skip to content

Commit 4380db7

Browse files
authored
BUG: avoid deadlocks using NpyString API (numpy#31682)
For NpyString_acquire_allocators and NpyString_release_allocators, the fix is to sort the mutexes by address before locking. I added tests for ufuncs that need 2, 3, and 4 allocators as well as one that uses a duplicate allocator that all deadlock on main and don't with the fixes here. Also releases the string allocator mutex before grabbing the GIL, as this in theory not necessary safe if arbitrary Python could be running (there are more paths that would need this fix, though).
1 parent 2a40aa3 commit 4380db7

8 files changed

Lines changed: 384 additions & 66 deletions

File tree

‎doc/source/reference/c-api/strings.rst‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ Functions
188188
Acquire the mutex locking the allocator attached to
189189
``descr``. ``NpyString_release_allocator`` must be called on the allocator
190190
returned by this function exactly once. Note that functions requiring the
191-
GIL should not be called while the allocator mutex is held, as doing so may
192-
cause deadlocks.
191+
GIL should not be called while the allocator mutex is held unless
192+
re-entrancy is impossible, as doing so may cause deadlocks.
193193
194194
.. c:function:: void NpyString_acquire_allocators( \
195195
size_t n_descriptors, PyArray_Descr *const descrs[], \
@@ -212,7 +212,8 @@ Functions
212212
``NpyString_release_allocators``.
213213
214214
Note that functions requiring the GIL should not be called while the
215-
allocator mutex is held, as doing so may cause deadlocks.
215+
allocator mutex is held and reentrancy is possible, as doing so may cause
216+
deadlocks.
216217
217218
.. c:function:: void NpyString_release_allocator( \
218219
npy_string_allocator *allocator)

‎numpy/_core/src/common/raii_utils.hpp‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class NpyStringAcquireAllocator
148148
}
149149

150150
~NpyStringAcquireAllocator() {
151-
NpyString_release_allocator(_allocator);
151+
release();
152152
}
153153

154154
NpyStringAcquireAllocator(const NpyStringAcquireAllocator&) = delete;
@@ -159,6 +159,18 @@ class NpyStringAcquireAllocator
159159
npy_string_allocator *allocator() {
160160
return _allocator;
161161
}
162+
163+
// Release the allocator lock early, before this object goes out of scope.
164+
// Use this when a scope must drop the lock partway through, e.g. before
165+
// running Python C API code that must not hold the allocator lock (which
166+
// could otherwise deadlock on re-entrancy). Idempotent: the destructor
167+
// also calls it, so an explicit call never double-releases.
168+
void release() {
169+
if (_allocator != NULL) {
170+
NpyString_release_allocator(_allocator);
171+
_allocator = NULL;
172+
}
173+
}
162174
};
163175

164176
}} // namespace np { namespace raii {

‎numpy/_core/src/multiarray/stringdtype/casts.cpp‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,8 +1918,6 @@ string_to_bytes(PyArrayMethod_Context *context, char *const data[],
19181918
NpyAuxData *NPY_UNUSED(auxdata))
19191919
{
19201920
PyArray_StringDTypeObject *descr = (PyArray_StringDTypeObject *)context->descriptors[0];
1921-
np::raii::NpyStringAcquireAllocator alloc(descr);
1922-
19231921
int has_null = descr->na_object != NULL;
19241922
int has_string_na = descr->has_string_na;
19251923
const npy_static_string *default_string = &descr->default_string;
@@ -1931,6 +1929,8 @@ string_to_bytes(PyArrayMethod_Context *context, char *const data[],
19311929
npy_intp out_stride = strides[1];
19321930
size_t max_out_size = context->descriptors[1]->elsize;
19331931

1932+
np::raii::NpyStringAcquireAllocator alloc(descr);
1933+
19341934
while (N--) {
19351935
const npy_packed_static_string *ps = (npy_packed_static_string *)in;
19361936
npy_static_string s = {0, NULL};
@@ -1940,11 +1940,29 @@ string_to_bytes(PyArrayMethod_Context *context, char *const data[],
19401940
return -1;
19411941
}
19421942

1943-
for (size_t i=0; i<s.size; i++) {
1943+
for (size_t i = 0; i < s.size; i++) {
19441944
if (((unsigned char *)s.buf)[i] > 127) {
1945+
// Building the UnicodeEncodeError needs the GIL and must not
1946+
// run while the allocator is held (re-entrant Python could
1947+
// deadlock on it), so copy the offending bytes out, release the
1948+
// allocator, then raise.
1949+
char *bad = (char *)PyMem_RawMalloc(s.size);
1950+
if (bad == NULL) {
1951+
alloc.release();
1952+
npy_gil_error(PyExc_MemoryError,
1953+
"Failed to allocate memory for unicode "
1954+
"encoding error");
1955+
return -1;
1956+
}
1957+
memcpy(bad, s.buf, s.size);
1958+
size_t bad_size = s.size;
1959+
size_t bad_pos = i;
1960+
alloc.release();
1961+
19451962
np::raii::EnsureGIL ensure_gil{};
19461963

1947-
PyObject *str = PyUnicode_FromStringAndSize(s.buf, s.size);
1964+
PyObject *str = PyUnicode_FromStringAndSize(bad, bad_size);
1965+
PyMem_RawFree(bad);
19481966

19491967
if (str == NULL) {
19501968
PyErr_SetString(
@@ -1958,8 +1976,8 @@ string_to_bytes(PyArrayMethod_Context *context, char *const data[],
19581976
"sOnns",
19591977
"ascii",
19601978
str,
1961-
(Py_ssize_t)i,
1962-
(Py_ssize_t)(i+1),
1979+
(Py_ssize_t)bad_pos,
1980+
(Py_ssize_t)(bad_pos + 1),
19631981
"ordinal not in range(128)"
19641982
);
19651983

‎numpy/_core/src/multiarray/stringdtype/dtype.c‎

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -315,62 +315,55 @@ stringdtype_setitem(PyArray_StringDTypeObject *descr, PyObject *obj, char **data
315315
{
316316
npy_packed_static_string *sdata = (npy_packed_static_string *)dataptr;
317317

318-
// borrow reference
318+
// borrowed reference
319319
PyObject *na_object = descr->na_object;
320320

321-
// We need the result of the comparison after acquiring the allocator, but
322-
// cannot use functions requiring the GIL when the allocator is acquired,
323-
// so we do the comparison before acquiring the allocator.
324-
321+
// We need the result of the comparison before packing below, but cannot
322+
// use functions requiring the GIL when the allocator is acquired.
325323
int na_cmp = na_eq_cmp(obj, na_object);
326324
if (na_cmp == -1) {
327325
return -1;
328326
}
329327

330-
npy_string_allocator *allocator = NpyString_acquire_allocator(descr);
331-
332-
if (na_object != NULL) {
333-
if (na_cmp) {
334-
if (NpyString_pack_null(allocator, sdata) < 0) {
335-
PyErr_SetString(PyExc_MemoryError,
336-
"Failed to pack null string during StringDType "
337-
"setitem");
338-
goto fail;
339-
}
340-
goto success;
328+
if (na_object != NULL && na_cmp) {
329+
npy_string_allocator *allocator = NpyString_acquire_allocator(descr);
330+
int pack_status = NpyString_pack_null(allocator, sdata);
331+
NpyString_release_allocator(allocator);
332+
if (pack_status < 0) {
333+
PyErr_SetString(PyExc_MemoryError,
334+
"Failed to pack null string during StringDType "
335+
"setitem");
336+
return -1;
341337
}
338+
return 0;
342339
}
340+
343341
PyObject *val_obj = as_pystring(obj, descr->coerce);
344342

345343
if (val_obj == NULL) {
346-
goto fail;
344+
return -1;
347345
}
348346

349347
Py_ssize_t length = 0;
350348
const char *val = PyUnicode_AsUTF8AndSize(val_obj, &length);
351349
if (val == NULL) {
352350
Py_DECREF(val_obj);
353-
goto fail;
351+
return -1;
354352
}
355353

356-
if (NpyString_pack(allocator, sdata, val, length) < 0) {
354+
npy_string_allocator *allocator = NpyString_acquire_allocator(descr);
355+
int pack_status = NpyString_pack(allocator, sdata, val, length);
356+
NpyString_release_allocator(allocator);
357+
Py_DECREF(val_obj);
358+
359+
if (pack_status < 0) {
357360
PyErr_SetString(PyExc_MemoryError,
358361
"Failed to pack string during StringDType "
359362
"setitem");
360-
Py_DECREF(val_obj);
361-
goto fail;
363+
return -1;
362364
}
363-
Py_DECREF(val_obj);
364-
365-
success:
366-
NpyString_release_allocator(allocator);
367365

368366
return 0;
369-
370-
fail:
371-
NpyString_release_allocator(allocator);
372-
373-
return -1;
374367
}
375368

376369
static PyObject *

‎numpy/_core/src/multiarray/stringdtype/static_string.c‎

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,37 @@ arena_free(npy_string_arena *arena, _npy_static_string_u *str)
229229

230230
static npy_string_arena NEW_ARENA = {0, 0, NULL};
231231

232+
static void
233+
acquire_allocator_lock(npy_string_allocator *allocator)
234+
{
235+
#if PY_VERSION_HEX < 0x30d00b3
236+
if (!PyThread_acquire_lock(allocator->allocator_lock, NOWAIT_LOCK)) {
237+
if (PyGILState_Check()) {
238+
Py_BEGIN_ALLOW_THREADS
239+
PyThread_acquire_lock(allocator->allocator_lock, WAIT_LOCK);
240+
Py_END_ALLOW_THREADS
241+
}
242+
else {
243+
PyThread_acquire_lock(allocator->allocator_lock, WAIT_LOCK);
244+
}
245+
}
246+
#else
247+
PyMutex_Lock(&allocator->allocator_lock);
248+
#endif
249+
}
250+
251+
static int
252+
allocator_seen(npy_string_allocator *allocator,
253+
npy_string_allocator *allocators[], size_t end)
254+
{
255+
for (size_t i = 0; i < end; i++) {
256+
if (allocators[i] == allocator) {
257+
return 1;
258+
}
259+
}
260+
return 0;
261+
}
262+
232263
NPY_NO_EXPORT npy_string_allocator *
233264
NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f,
234265
npy_string_realloc_func r)
@@ -281,18 +312,13 @@ NpyString_free_allocator(npy_string_allocator *allocator)
281312
* by this function exactly once.
282313
*
283314
* Note that functions requiring the GIL should not be called while the
284-
* allocator mutex is held, as doing so may cause deadlocks.
315+
* allocator mutex is held and reentrant calls are possible, as doing so may
316+
* cause deadlocks.
285317
*/
286318
NPY_NO_EXPORT npy_string_allocator *
287319
NpyString_acquire_allocator(const PyArray_StringDTypeObject *descr)
288320
{
289-
#if PY_VERSION_HEX < 0x30d00b3
290-
if (!PyThread_acquire_lock(descr->allocator->allocator_lock, NOWAIT_LOCK)) {
291-
PyThread_acquire_lock(descr->allocator->allocator_lock, WAIT_LOCK);
292-
}
293-
#else
294-
PyMutex_Lock(&descr->allocator->allocator_lock);
295-
#endif
321+
acquire_allocator_lock(descr->allocator);
296322
return descr->allocator;
297323
}
298324

@@ -309,42 +335,64 @@ NpyString_acquire_allocator(const PyArray_StringDTypeObject *descr)
309335
* ignored. A buffer overflow will happen if the *descrs* array does not
310336
* contain n_descriptors elements.
311337
*
312-
* If pointers to the same descriptor are passed multiple times, only acquires
338+
* If pointers to the same allocator are passed multiple times, only acquires
313339
* the allocator mutex once but sets identical allocator pointers appropriately.
340+
* Distinct allocator mutexes are acquired in ascending allocator address order
341+
* to avoid lock-ordering deadlocks when callers pass different descriptor
342+
* orders.
314343
*
315344
* The allocator mutexes must be released after this function returns, see
316345
* NpyString_release_allocators.
317346
*
318347
* Note that functions requiring the GIL should not be called while the
319-
* allocator mutex is held, as doing so may cause deadlocks.
348+
* allocator mutex is held and reentrant calls are possible, as doing so may
349+
* cause deadlocks.
320350
*/
321351
NPY_NO_EXPORT void
322352
NpyString_acquire_allocators(size_t n_descriptors,
323353
PyArray_Descr *const descrs[],
324354
npy_string_allocator *allocators[])
325355
{
326-
for (size_t i=0; i<n_descriptors; i++) {
356+
for (size_t i = 0; i < n_descriptors; i++) {
327357
if (NPY_DTYPE(descrs[i]) != &PyArray_StringDType) {
328358
allocators[i] = NULL;
329359
continue;
330360
}
331-
int allocators_match = 0;
332-
for (size_t j=0; j<i; j++) {
333-
if (allocators[j] == NULL) {
361+
allocators[i] = ((PyArray_StringDTypeObject *)descrs[i])->allocator;
362+
}
363+
364+
uintptr_t last_locked_addr = 0;
365+
int have_last_locked_addr = 0;
366+
367+
for (;;) {
368+
npy_string_allocator *next_allocator = NULL;
369+
uintptr_t next_addr = UINTPTR_MAX;
370+
371+
for (size_t i = 0; i < n_descriptors; i++) {
372+
npy_string_allocator *allocator = allocators[i];
373+
374+
if (allocator == NULL || allocator_seen(allocator, allocators, i)) {
334375
continue;
335376
}
336-
if (((PyArray_StringDTypeObject *)descrs[i])->allocator ==
337-
((PyArray_StringDTypeObject *)descrs[j])->allocator)
338-
{
339-
allocators[i] = allocators[j];
340-
allocators_match = 1;
341-
break;
377+
378+
uintptr_t addr = (uintptr_t)allocator;
379+
380+
if (have_last_locked_addr && addr <= last_locked_addr) {
381+
continue;
382+
}
383+
if (next_allocator == NULL || addr < next_addr) {
384+
next_allocator = allocator;
385+
next_addr = addr;
342386
}
343387
}
344-
if (!allocators_match) {
345-
allocators[i] = NpyString_acquire_allocator(
346-
(PyArray_StringDTypeObject *)descrs[i]);
388+
389+
if (next_allocator == NULL) {
390+
break;
347391
}
392+
393+
acquire_allocator_lock(next_allocator);
394+
last_locked_addr = next_addr;
395+
have_last_locked_addr = 1;
348396
}
349397
}
350398

‎numpy/_core/src/multiarray/unique.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,8 @@ unique_vstring(PyArrayObject *self, npy_bool equal_nan)
357357
{
358358
PyArray_StringDTypeObject *descr =
359359
reinterpret_cast<PyArray_StringDTypeObject *>(PyArray_DESCR(self));
360-
np::raii::NpyStringAcquireAllocator alloc(descr);
361360
np::raii::SaveThreadState save_thread_state{};
361+
np::raii::NpyStringAcquireAllocator alloc(descr);
362362

363363
char *idata = PyArray_BYTES(self);
364364
npy_intp istride = PyArray_STRIDES(self)[0];
@@ -387,8 +387,8 @@ unique_vstring(PyArrayObject *self, npy_bool equal_nan)
387387
{
388388
PyArray_StringDTypeObject *res_descr =
389389
reinterpret_cast<PyArray_StringDTypeObject *>(PyArray_DESCR(res_obj));
390-
np::raii::NpyStringAcquireAllocator alloc(res_descr);
391390
np::raii::SaveThreadState save_thread_state{};
391+
np::raii::NpyStringAcquireAllocator alloc(res_descr);
392392

393393
char *odata = PyArray_BYTES(res_obj);
394394
npy_intp ostride = PyArray_STRIDES(res_obj)[0];

0 commit comments

Comments
 (0)