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

Skip to content

BUG: fix stringdtype singleton thread safety #28864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions numpy/_core/src/multiarray/stringdtype/dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,11 +633,16 @@ PyArray_Descr *
stringdtype_finalize_descr(PyArray_Descr *dtype)
{
PyArray_StringDTypeObject *sdtype = (PyArray_StringDTypeObject *)dtype;
// acquire the allocator lock in case the descriptor we want to finalize
// is shared between threads, see gh-28813
npy_string_allocator *allocator = NpyString_acquire_allocator(sdtype);
if (sdtype->array_owned == 0) {
sdtype->array_owned = 1;
NpyString_release_allocator(allocator);
Py_INCREF(dtype);
return dtype;
}
NpyString_release_allocator(allocator);
PyArray_StringDTypeObject *ret = (PyArray_StringDTypeObject *)new_stringdtype_instance(
sdtype->na_object, sdtype->coerce);
ret->array_owned = 1;
Expand Down Expand Up @@ -852,14 +857,17 @@ init_string_dtype(void)
return -1;
}

PyArray_Descr *singleton =
NPY_DT_CALL_default_descr(&PyArray_StringDType);
PyArray_StringDTypeObject *singleton =
(PyArray_StringDTypeObject *)NPY_DT_CALL_default_descr(&PyArray_StringDType);

if (singleton == NULL) {
return -1;
}

PyArray_StringDType.singleton = singleton;
// never associate the singleton with an array
singleton->array_owned = 1;

PyArray_StringDType.singleton = (PyArray_Descr *)singleton;
PyArray_StringDType.type_num = NPY_VSTRING;

for (int i = 0; PyArray_StringDType_casts[i] != NULL; i++) {
Expand Down
Loading