Skip to content

Commit e492d61

Browse files
committed
Fix ThreadSanitizer data race conditions in NumPy core
This commit addresses multiple race conditions detected by ThreadSanitizer in issue numpy#30085: 1. Loop data cache race condition (legacy_array_method.c): - Added thread-safe mutex protection for global loop_data_cache - Protects concurrent access in get_new_loop_data() and cache cleanup - Uses PyMutex (Python 3.13+) and PyThread_type_lock (older versions) 2. Coercion cache race condition (array_coercion.c): - Added thread-safe mutex protection for global _coercion_cache_cache - Protects concurrent access in npy_unlink_coercion_cache() - Prevents race conditions during array type coercion 3. Enhanced np.nonzero race detection (item_selection.c): - Extended existing race detection to cover boolean array fast paths - Added mutation detection to previously unprotected code paths - Ensures consistent error reporting across all array types All fixes follow NumPy's existing mutex patterns and maintain compatibility with both GIL-enabled and free-threaded Python execution modes. Fixes: numpygh-30085
1 parent 7612da0 commit e492d61

7 files changed

Lines changed: 105 additions & 19 deletions

File tree

‎numpy/_core/src/common/umathmodule.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ get_sfloat_dtype(PyObject *NPY_UNUSED(mod), PyObject *NPY_UNUSED(args));
1212
PyObject * add_newdoc_ufunc(PyObject *NPY_UNUSED(dummy), PyObject *args);
1313
PyObject * ufunc_frompyfunc(PyObject *NPY_UNUSED(dummy), PyObject *args, PyObject *NPY_UNUSED(kwds));
1414

15+
NPY_NO_EXPORT int init_loop_data_cache_mutex(void);
1516

1617
int initumath(PyObject *m);
1718

‎numpy/_core/src/multiarray/array_coercion.c‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -610,12 +610,21 @@ update_shape(int curr_ndim, int *max_ndim,
610610
return success;
611611
}
612612

613-
#ifndef Py_GIL_DISABLED
614613
#define COERCION_CACHE_CACHE_SIZE 5
615614
static int _coercion_cache_num = 0;
616615
static coercion_cache_obj *_coercion_cache_cache[COERCION_CACHE_CACHE_SIZE];
616+
617+
/* Thread-safe cache access using mutex */
618+
#if PY_VERSION_HEX < 0x30d00b3
619+
static PyThread_type_lock coercion_cache_mutex;
620+
#define LOCK_COERCION_CACHE \
621+
if (coercion_cache_mutex) PyThread_acquire_lock(coercion_cache_mutex, WAIT_LOCK)
622+
#define UNLOCK_COERCION_CACHE \
623+
if (coercion_cache_mutex) PyThread_release_lock(coercion_cache_mutex)
617624
#else
618-
#define COERCION_CACHE_CACHE_SIZE 0
625+
static PyMutex coercion_cache_mutex = {0};
626+
#define LOCK_COERCION_CACHE PyMutex_Lock(&coercion_cache_mutex)
627+
#define UNLOCK_COERCION_CACHE PyMutex_Unlock(&coercion_cache_mutex)
619628
#endif
620629

621630
/*
@@ -628,15 +637,19 @@ npy_new_coercion_cache(
628637
{
629638
coercion_cache_obj *cache;
630639
#if COERCION_CACHE_CACHE_SIZE > 0
640+
LOCK_COERCION_CACHE;
631641
if (_coercion_cache_num > 0) {
632642
_coercion_cache_num--;
633643
cache = _coercion_cache_cache[_coercion_cache_num];
644+
UNLOCK_COERCION_CACHE;
634645
}
635-
else
636-
#endif
637-
{
646+
else {
647+
UNLOCK_COERCION_CACHE;
638648
cache = PyMem_Malloc(sizeof(coercion_cache_obj));
639649
}
650+
#else
651+
cache = PyMem_Malloc(sizeof(coercion_cache_obj));
652+
#endif
640653
if (cache == NULL) {
641654
Py_DECREF(arr_or_sequence);
642655
PyErr_NoMemory();
@@ -664,15 +677,19 @@ npy_unlink_coercion_cache(coercion_cache_obj *current)
664677
coercion_cache_obj *next = current->next;
665678
Py_DECREF(current->arr_or_sequence);
666679
#if COERCION_CACHE_CACHE_SIZE > 0
680+
LOCK_COERCION_CACHE;
667681
if (_coercion_cache_num < COERCION_CACHE_CACHE_SIZE) {
668682
_coercion_cache_cache[_coercion_cache_num] = current;
669683
_coercion_cache_num++;
684+
UNLOCK_COERCION_CACHE;
670685
}
671-
else
672-
#endif
673-
{
686+
else {
687+
UNLOCK_COERCION_CACHE;
674688
PyMem_Free(current);
675689
}
690+
#else
691+
PyMem_Free(current);
692+
#endif
676693
return next;
677694
}
678695

@@ -1443,3 +1460,15 @@ _discover_array_parameters(PyObject *NPY_UNUSED(self),
14431460
Py_DECREF(shape_tuple);
14441461
return res;
14451462
}
1463+
1464+
NPY_NO_EXPORT int
1465+
init_coercion_cache_mutex(void) {
1466+
#if PY_VERSION_HEX < 0x30d00b3
1467+
coercion_cache_mutex = PyThread_allocate_lock();
1468+
if (coercion_cache_mutex == NULL) {
1469+
PyErr_NoMemory();
1470+
return -1;
1471+
}
1472+
#endif
1473+
return 0;
1474+
}

‎numpy/_core/src/multiarray/array_coercion.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,7 @@ npy_unlink_coercion_cache(coercion_cache_obj *current);
5858
NPY_NO_EXPORT int
5959
PyArray_AssignFromCache(PyArrayObject *self, coercion_cache_obj *cache);
6060

61+
NPY_NO_EXPORT int
62+
init_coercion_cache_mutex(void);
63+
6164
#endif /* NUMPY_CORE_SRC_MULTIARRAY_ARRAY_COERCION_H_ */

‎numpy/_core/src/multiarray/item_selection.c‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2900,6 +2900,9 @@ PyArray_Nonzero(PyArrayObject *self)
29002900
if (j >= count) {
29012901
break;
29022902
}
2903+
if (++added_count > nonzero_count) {
2904+
break;
2905+
}
29032906
*multi_index++ = j++;
29042907
}
29052908
}
@@ -2914,19 +2917,24 @@ PyArray_Nonzero(PyArrayObject *self)
29142917
while (multi_index + 4 < multi_index_end && (j < count - 4) ) {
29152918
*multi_index = j;
29162919
multi_index += data[0] != 0;
2920+
if (data[0] != 0 && ++added_count > nonzero_count) break;
29172921
*multi_index = j + 1;
29182922
multi_index += data[stride] != 0;
2923+
if (data[stride] != 0 && ++added_count > nonzero_count) break;
29192924
*multi_index = j + 2;
29202925
multi_index += data[stride * 2] != 0;
2926+
if (data[stride * 2] != 0 && ++added_count > nonzero_count) break;
29212927
*multi_index = j + 3;
29222928
multi_index += data[stride * 3] != 0;
2929+
if (data[stride * 3] != 0 && ++added_count > nonzero_count) break;
29232930
data += stride * 4;
29242931
j += 4;
29252932
}
29262933

29272934
while (multi_index < multi_index_end && (j < count) ) {
29282935
*multi_index = j;
29292936
multi_index += *data != 0;
2937+
if (*data != 0 && ++added_count > nonzero_count) break;
29302938
data += stride;
29312939
++j;
29322940
}
@@ -3001,6 +3009,9 @@ PyArray_Nonzero(PyArrayObject *self)
30013009
/* avoid function call for bool */
30023010
do {
30033011
if (**dataptr != 0) {
3012+
if (++added_count > nonzero_count) {
3013+
break;
3014+
}
30043015
get_multi_index(iter, multi_index);
30053016
multi_index += ndim;
30063017
}
@@ -3032,8 +3043,8 @@ PyArray_Nonzero(PyArrayObject *self)
30323043
return NULL;
30333044
}
30343045

3035-
/* if executed `nonzero()` check for miscount due to side-effect */
3036-
if (!is_bool && added_count != nonzero_count) {
3046+
/* check for miscount due to side-effect (race condition) */
3047+
if (added_count != nonzero_count) {
30373048
PyErr_SetString(PyExc_RuntimeError,
30383049
"number of non-zero array elements "
30393050
"changed during function execution.");

‎numpy/_core/src/multiarray/multiarraymodule.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,6 +4946,10 @@ _multiarray_umath_exec(PyObject *m) {
49464946
return -1;
49474947
}
49484948

4949+
if (init_coercion_cache_mutex() < 0) {
4950+
return -1;
4951+
}
4952+
49494953
if (init_extobj() < 0) {
49504954
return -1;
49514955
}

‎numpy/_core/src/umath/legacy_array_method.c‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,41 @@ typedef struct {
3333

3434

3535
/* Use a free list, since we should normally only need one at a time */
36-
#ifndef Py_GIL_DISABLED
3736
#define NPY_LOOP_DATA_CACHE_SIZE 5
3837
static int loop_data_num_cached = 0;
39-
static legacy_array_method_auxdata *loop_data_cache[NPY_LOOP_DATA_CACHE_SIZE];
38+
static legacy_array_method_auxdata *loop_data_cache[NPY_LOOP_DATA_CACHE_SIZE];
39+
40+
/* Thread-safe cache access using mutex */
41+
#if PY_VERSION_HEX < 0x30d00b3
42+
static PyThread_type_lock loop_data_cache_mutex;
43+
#define LOCK_LOOP_DATA_CACHE \
44+
if (loop_data_cache_mutex) PyThread_acquire_lock(loop_data_cache_mutex, WAIT_LOCK)
45+
#define UNLOCK_LOOP_DATA_CACHE \
46+
if (loop_data_cache_mutex) PyThread_release_lock(loop_data_cache_mutex)
4047
#else
41-
#define NPY_LOOP_DATA_CACHE_SIZE 0
48+
static PyMutex loop_data_cache_mutex = {0};
49+
#define LOCK_LOOP_DATA_CACHE PyMutex_Lock(&loop_data_cache_mutex)
50+
#define UNLOCK_LOOP_DATA_CACHE PyMutex_Unlock(&loop_data_cache_mutex)
4251
#endif
4352

4453
static void
4554
legacy_array_method_auxdata_free(NpyAuxData *data)
4655
{
4756
#if NPY_LOOP_DATA_CACHE_SIZE > 0
57+
LOCK_LOOP_DATA_CACHE;
4858
if (loop_data_num_cached < NPY_LOOP_DATA_CACHE_SIZE) {
4959
loop_data_cache[loop_data_num_cached] = (
5060
(legacy_array_method_auxdata *)data);
5161
loop_data_num_cached++;
62+
UNLOCK_LOOP_DATA_CACHE;
5263
}
53-
else
54-
#endif
55-
{
64+
else {
65+
UNLOCK_LOOP_DATA_CACHE;
5666
PyMem_Free(data);
5767
}
68+
#else
69+
PyMem_Free(data);
70+
#endif
5871
}
5972

6073
NpyAuxData *
@@ -63,20 +76,29 @@ get_new_loop_data(
6376
{
6477
legacy_array_method_auxdata *data;
6578
#if NPY_LOOP_DATA_CACHE_SIZE > 0
79+
LOCK_LOOP_DATA_CACHE;
6680
if (NPY_LIKELY(loop_data_num_cached > 0)) {
6781
loop_data_num_cached--;
6882
data = loop_data_cache[loop_data_num_cached];
83+
UNLOCK_LOOP_DATA_CACHE;
6984
}
70-
else
71-
#endif
72-
{
85+
else {
86+
UNLOCK_LOOP_DATA_CACHE;
7387
data = PyMem_Malloc(sizeof(legacy_array_method_auxdata));
7488
if (data == NULL) {
7589
return NULL;
7690
}
7791
data->base.free = legacy_array_method_auxdata_free;
7892
data->base.clone = NULL; /* no need for cloning (at least for now) */
7993
}
94+
#else
95+
data = PyMem_Malloc(sizeof(legacy_array_method_auxdata));
96+
if (data == NULL) {
97+
return NULL;
98+
}
99+
data->base.free = legacy_array_method_auxdata_free;
100+
data->base.clone = NULL; /* no need for cloning (at least for now) */
101+
#endif
80102
data->loop = loop;
81103
data->user_data = user_data;
82104
data->pyerr_check = pyerr_check;
@@ -85,6 +107,18 @@ get_new_loop_data(
85107

86108
#undef NPY_LOOP_DATA_CACHE_SIZE
87109

110+
NPY_NO_EXPORT int
111+
init_loop_data_cache_mutex(void) {
112+
#if PY_VERSION_HEX < 0x30d00b3
113+
loop_data_cache_mutex = PyThread_allocate_lock();
114+
if (loop_data_cache_mutex == NULL) {
115+
PyErr_NoMemory();
116+
return -1;
117+
}
118+
#endif
119+
return 0;
120+
}
121+
88122
/*
89123
* This is a thin wrapper around the legacy loop signature.
90124
*/

‎numpy/_core/src/umath/umathmodule.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,9 @@ int initumath(PyObject *m)
333333
return -1;
334334
}
335335

336+
if (init_loop_data_cache_mutex() < 0) {
337+
return -1;
338+
}
339+
336340
return 0;
337341
}

0 commit comments

Comments
 (0)