Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5870d71

Browse files
authored
Merge pull request #28864 from charris/backport-28862
BUG: fix stringdtype singleton thread safety
2 parents 7d1ebbe + 71e2b9c commit 5870d71

File tree

1 file changed

+11
-3
lines changed
  • numpy/_core/src/multiarray/stringdtype

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,11 +633,16 @@ PyArray_Descr *
633633
stringdtype_finalize_descr(PyArray_Descr *dtype)
634634
{
635635
PyArray_StringDTypeObject *sdtype = (PyArray_StringDTypeObject *)dtype;
636+
// acquire the allocator lock in case the descriptor we want to finalize
637+
// is shared between threads, see gh-28813
638+
npy_string_allocator *allocator = NpyString_acquire_allocator(sdtype);
636639
if (sdtype->array_owned == 0) {
637640
sdtype->array_owned = 1;
641+
NpyString_release_allocator(allocator);
638642
Py_INCREF(dtype);
639643
return dtype;
640644
}
645+
NpyString_release_allocator(allocator);
641646
PyArray_StringDTypeObject *ret = (PyArray_StringDTypeObject *)new_stringdtype_instance(
642647
sdtype->na_object, sdtype->coerce);
643648
ret->array_owned = 1;
@@ -852,14 +857,17 @@ init_string_dtype(void)
852857
return -1;
853858
}
854859

855-
PyArray_Descr *singleton =
856-
NPY_DT_CALL_default_descr(&PyArray_StringDType);
860+
PyArray_StringDTypeObject *singleton =
861+
(PyArray_StringDTypeObject *)NPY_DT_CALL_default_descr(&PyArray_StringDType);
857862

858863
if (singleton == NULL) {
859864
return -1;
860865
}
861866

862-
PyArray_StringDType.singleton = singleton;
867+
// never associate the singleton with an array
868+
singleton->array_owned = 1;
869+
870+
PyArray_StringDType.singleton = (PyArray_Descr *)singleton;
863871
PyArray_StringDType.type_num = NPY_VSTRING;
864872

865873
for (int i = 0; PyArray_StringDType_casts[i] != NULL; i++) {

0 commit comments

Comments
 (0)