8000 Merge pull request #28862 from ngoldbaum/stringdtype-singleton-fixes · r-devulap/numpy@1672b69 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1672b69

Browse files
authored
Merge pull request numpy#28862 from ngoldbaum/stringdtype-singleton-fixes
BUG: fix stringdtype singleton thread safety
2 parents 80d5607 + aed1f5b commit 1672b69

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;
@@ -850,14 +855,17 @@ init_string_dtype(void)
850855
return -1;
851856
}
852857

853-
PyArray_Descr *singleton =
854-
NPY_DT_CALL_default_descr(&PyArray_StringDType);
858+
PyArray_StringDTypeObject *singleton =
859+
(PyArray_StringDTypeObject *)NPY_DT_CALL_default_descr(&PyArray_StringDType);
855860

856861
if (singleton == NULL) {
857862
return -1;
858863
}
859864

860-
PyArray_StringDType.singleton = singleton;
865+
// never associate the singleton with an array
866+
singleton->array_owned = 1;
867+
868+
PyArray_StringDType.singleton = (PyArray_Descr *)singleton;
861869
PyArray_StringDType.type_num = NPY_VSTRING;
862870

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

0 commit comments

Comments
 (0)
0