GeezerVison is showing github.com — BUG: np.searchsorted gives flaky results with StringDType arrays · Issue #31533 · numpy/numpy
BUG: np.searchsorted gives flaky results with StringDType arrays #31533DescriptionDescribe the issue:
np.searchsorted with string arrays sometimes gives inconsistent results when run several times:
The problem is intermittent: The script below fails quickly in main in a fresh terminal, and stops failing after several reruns. Open a new terminal, and it fails again.
Reproduce the code example:
$ cat np.search.str.py
import numpy as np
print(np.__version__)
a = np.array(['a'*36, 'b'*36, 'c'*17], dtype='T')
res = np.searchsorted(a, 'c'*17)
for _ in range(3_000_000):
this = np.searchsorted(a, 'c'*17)
assert this == res, f"Failed in {_} run: {this} != {res}"
# and again
for _ in range(1000_000):
this = np.searchsorted(a, 'c'*17)
assert this == res, f"Failed in {_} run: {this} != {res}"
# and again
for _ in range(1000_000):
this = np.searchsorted(a, 'c'*17)
assert this == res, f"Failed in {_} run: {this} != {res}"
# and again
for _ in range(1000_000):
this = np.searchsorted(a, 'c'*17)
assert this == res, f"Failed in {_} run: {this} != {res}"
Error message:
$ spin python np.search.str.py
Invoking `build` prior to invoking Python:
$ /home/br/miniforge3/envs/numpy-dev/bin/python3.12 vendored-meson/meson/meson.py compile -C build
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: /home/br/miniforge3/envs/numpy-dev/bin/ninja -C /home/br/repos/numpy/build
ninja: Entering directory `/home/br/repos/numpy/build'
[1/1] Generating numpy/generate-version with a custom command
Saving version to numpy/version.py
$ /home/br/miniforge3/envs/numpy-dev/bin/python3.12 vendored-meson/meson/meson.py install --only-changed -C build --destdir ../build-install
$ export PYTHONPATH="/home/br/repos/numpy/build-install/usr/lib/python3.12/site-packages"
🐍 Launching Python with PYTHONPATH="/home/br/repos/numpy/build-install/usr/lib/python3.12/site-packages"
$ /home/br/miniforge3/envs/numpy-dev/bin/python3.12 -P np.search.str.py
2.6.0.dev0+git20260527.a20ef19
Traceback (most recent call last):
File "/home/br/repos/numpy/np.search.str.py", line 9, in <module>
assert this == res, f"Failed in {_} run: {this} != {res}"
^^^^^^^^^^^
AssertionError: Failed in 0 run: 3 != 2
Python and NumPy Versions:
Observed with
2.6.0.dev0+git20260527.a20ef19
3.12.10 | packaged by conda-forge | (main, Apr 10 2025, 22:21:13) [GCC 13.3.0]
and python 3.11 with numpy versions down to 2.4.1 (it seems to fail less with 2.4.1 than 2.4.6 but that might not mean anything)
Runtime Environment:
No response
How does this issue affect you or how did you find it:
No response Activityngoldbaum commented on May 29, 2026 I can't reproduce this on main. I can reproduce it with the 2.4.6 wheel.
This sort of smells like #31415, which should have been fixed by #31456 (backported to 2.4.6 in #31460).
Let me try with a debug python interpreter and a from-source build of 2.4.6... ev-br commented on May 29, 2026 ContributorAuthor More actions
I can't reproduce this on main
Rerun the reproducer multiple times? For me on Linux at least, it takes multiple reruns depending on... (Mercury vs Uranus?), and typically if it does not reproduce, opening a new terminal and rerunning in it is often enough. ngoldbaum commented on May 29, 2026
Rerun the reproducer multiple times?
Good call, it does fail on main for me on my Mac after 10 or so iterations:
while spin python ../test/test-std-search.py; do done;
ngoldbaum commented on May 29, 2026 ASAN detects an OOB read:
=================================================================
==89551==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000006786 at pc 0x000102f27d88 bp 0x00016d738870 sp 0x00016d738008
READ of size 1 at 0x604000006786 thread T0
#0 0x000102f27d84 in strncmp+0x55c (libclang_rt.asan_osx_dynamic.dylib:arm64+0x17d84)
#1 0x00010c2aac34 in NpyString_cmp static_string.c:731
#2 0x00010c2a5cc0 in _compare dtype.c:512
#3 0x00010c2a5ac4 in compare dtype.c:452
#4 0x00010c39e56c in void npy_binsearch<(side_t)0>(char const*, char const*, char*, long, long, long, long, long, tagPyArrayObject*) binsearch.cpp:275
#5 0x00010c1e95d0 in PyArray_SearchSorted item_selection.c:2199
#6 0x00010c221284 in array_searchsorted methods.c:1582
#7 0x0001048bf0d8 in cfunction_vectorcall_FASTCALL_KEYWORDS methodobject.c:465
#8 0x00010477c03c in _PyVectorcall_Call call.c:285
#9 0x00010477c2ac in _PyObject_Call call.c:348
#10 0x00010477c3d4 in PyObject_Call call.c:373
#11 0x000104ba6054 in _PyEval_EvalFrameDefault generated_cases.c.h:2656
#12 0x000104b7d424 in _PyEval_EvalFrame pycore_ceval.h:120
#13 0x000104b7d238 in _PyEval_Vector ceval.c:2110
#14 0x00010477c848 in _PyFunction_Vectorcall call.c:413
#15 0x00010477a310 in _PyObject_VectorcallTstate pycore_call.h:177
#16 0x00010477c154 in PyObject_Vectorcall call.c:327
#17 0x00010c112278 in dispatcher_vectorcall arrayfunction_override.c:580
#18 0x00010477a310 in _PyObject_VectorcallTstate pycore_call.h:177
#19 0x00010477c154 in PyObject_Vectorcall call.c:327
#20 0x000104b967c8 in _PyEval_EvalFrameDefault generated_cases.c.h:1621
#21 0x000104b7d424 in _PyEval_EvalFrame pycore_ceval.h:120
#22 0x000104b7d238 in _PyEval_Vector ceval.c:2110
#23 0x000104b7ccd0 in PyEval_EvalCode ceval.c:982
#24 0x000104e09d5c in run_eval_code_obj pythonrun.c:1366
#25 0x000104e09934 in run_mod pythonrun.c:1460
#26 0x000104e05ef0 in pyrun_file pythonrun.c:1294
#27 0x000104e04acc in _PyRun_SimpleFileObject pythonrun.c:521
#28 0x000104e04160 in _PyRun_AnyFileObject pythonrun.c:81
#29 0x000104eaa9d4 in pymain_run_file_obj main.c:410
#30 0x000104ea9258 in pymain_run_file main.c:429
#31 0x000104ea7910 in pymain_run_python main.c:695
#32 0x000104ea6f28 in Py_RunMain main.c:776
#33 0x000104ea7f28 in pymain_main main.c:806
#34 0x000104ea8130 in Py_BytesMain main.c:830
#35 0x000102694790 in main python.c:15
#36 0x000183533dfc in start+0x1b4c (dyld:arm64e+0x1fdfc)
0x604000006786 is located 8 bytes after 46-byte region [0x604000006750,0x60400000677e)
allocated by thread T0 here:
#0 0x000102f64e24 in malloc+0x70 (libclang_rt.asan_osx_dynamic.dylib:arm64+0x54e24)
#1 0x00010490b428 in _PyMem_RawMalloc obmalloc.c:63
#2 0x000104910018 in _PyMem_DebugRawAlloc obmalloc.c:2891
#3 0x00010490b9d4 in _PyMem_DebugRawRealloc obmalloc.c:2967
#4 0x00010490dc08 in PyMem_RawRealloc obmalloc.c:1037
#5 0x00010c2a9ffc in arena_malloc static_string.c:181
#6 0x00010c2a9ffc in heap_or_arena_allocate static_string.c:471
#7 0x00010c2a9ffc in NpyString_newemptysize static_string.c:567
#8 0x00010c2aaccc in NpyString_newsize static_string.c:603
#9 0x00010c2aaccc in NpyString_pack static_string.c:777
#10 0x00010c2a5360 in stringdtype_setitem dtype.c:356
#11 0x00010c15cec8 in PyArray_FromAny_int ctors.c:1681
#12 0x00010c15ec94 in PyArray_CheckFromAny_int ctors.c:1814
#13 0x00010c15e684 in PyArray_CheckFromAny ctors.c:1781
#14 0x00010c1e8c08 in PyArray_SearchSorted item_selection.c:2128
#15 0x00010c221284 in array_searchsorted methods.c:1582
#16 0x0001048bf0d8 in cfunction_vectorcall_FASTCALL_KEYWORDS methodobject.c:465
#17 0x00010477c03c in _PyVectorcall_Call call.c:285
#18 0x00010477c2ac in _PyObject_Call call.c:348
#19 0x00010477c3d4 in PyObject_Call call.c:373
#20 0x000104ba6054 in _PyEval_EvalFrameDefault generated_cases.c.h:2656
#21 0x000104b7d424 in _PyEval_EvalFrame pycore_ceval.h:120
#22 0x000104b7d238 in _PyEval_Vector ceval.c:2110
#23 0x00010477c848 in _PyFunction_Vectorcall call.c:413
#24 0x00010477a310 in _PyObject_VectorcallTstate pycore_call.h:177
#25 0x00010477c154 in PyObject_Vectorcall call.c:327
#26 0x00010c112278 in dispatcher_vectorcall arrayfunction_override.c:580
#27 0x00010477a310 in _PyObject_VectorcallTstate pycore_call.h:177
#28 0x00010477c154 in PyObject_Vectorcall call.c:327
#29 0x000104b967c8 in _PyEval_EvalFrameDefault generated_cases.c.h:1621
#30 0x000104b7d424 in _PyEval_EvalFrame pycore_ceval.h:120
#31 0x000104b7d238 in _PyEval_Vector ceval.c:2110
#32 0x000104b7ccd0 in PyEval_EvalCode ceval.c:982
SUMMARY: AddressSanitizer: heap-buffer-overflow static_string.c:731 in NpyString_cmp
Shadow bytes around the buggy address:
0x604000006500: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fd
0x604000006580: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fa
0x604000006600: fa fa fd fd fd fd fd fa fa fa fd fd fd fd fd fa
0x604000006680: fa fa fd fd fd fd fd fa fa fa 00 00 00 00 00 fa
0x604000006700: fa fa 00 00 00 00 00 fa fa fa 00 00 00 00 00 06
=>0x604000006780:[fa]fa 00 00 00 00 00 fa fa fa fa fa fa fa fa fa
0x604000006800: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x604000006880: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x604000006900: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x604000006980: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x604000006a00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==89551==ABORTING
Reacted by Jake Vanderplas ngoldbaum commented on May 29, 2026 Ultimately this comes down to searchsorted being wired up to assume that the needle and haystack arrays both share a dtype instance. That's true almost everywhere else but is not true for StringDType.
I think the cleanest fix is to add a new searchsorted slot following the sort and argsort slots we already have (ping @MaanasArora). That should go into 2.6 but it's almost certainly too late for 2.5 to fix that, so I think I'll do a more minimal hacky fix that doesn't require adding a new feature in the dtype API.
@seberg does that seem reasonable to you? Reacted by Sebastian Berg seberg commented on May 29, 2026 Yeah, that sounds good. We may want to consider briefly if reviving the generic sort-compare function makes sense. But that adds the interesting dimension that we need two dtype instances.
(But I am not sure that is better, making headers public to make it simple to implement the full searchsorted seems just as well; I might be curious if one could meld/LTO style based on a generic get_compare_function() but I am not convinced it's worth for this.)
Agreed that it would be nice to find a more narrow fix for searchsorted for 2.5. I guess it may be that the only thing that works is to introduce an explicit if stringdtype: custom_function()... ngoldbaum commented on May 29, 2026
Agreed that it would be nice to find a more narrow fix for searchsorted for 2.5. I guess it may be that the only thing that works is to introduce an explicit if stringdtype: custom_function()...
That's pretty much the patch I'm working on right now, along with a TODO to clean it up. Reacted by Sebastian Berg ngoldbaum commented on May 29, 2026
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|