Issue 1:
Describe your issue.

scipy.spatial.KDTree() throws Segmentation fault on given input.

Reproducing Code Example

import numpy as np
from scipy.spatial import KDTree

data = [
[456., 9.],
[572., 10.],
[555., 15.],
[604., 16.],
[547., 29.],
[34., 41.],
[490., 44.],
[590., 46.],
[566., 49.],
[38., 53.],
[519., 66.],
[535., 66.],
[512., 67.],
[572., 70.],
[104., 73.],
[47., 74.],
[559., 79.],
[617., 79.],
[136., 87.],
[206., 89.],
[545., 93.],
[573., 93.],
[510., 95.],
[149., 99.],
[96., 102.],
[504., 105.],
[119., 108.],
[100., 110.],
[526., 113.],
[598., 113.],
[493., 115.],
[487., 122.],
[473., 132.],
[521., 137.],
[477., 145.],
[405., 149.],
[451., 150.],
[490., 153.],
[420., 155.],
[406., 157.],
[386., 162.],
[508., 162.],
[377., 163.],
[366., 166.],
[357., 168.],
[397., 172.],
[371., 173.],
[322., 174.],
[424., 174.],
[376., 175.],
[405., 176.],
[346., 177.],
[296., 179.],
[339., 180.],
[353., 182.],
[323., 183.],
[382., 183.],
[255., 186.],
[290., 186.],
[331., 186.],
[276., 187.],
[405., 187.],
[302., 189.],
[316., 189.],
[336., 190.],
[393., 191.],
[230., 192.],
[285., 193.],
[214., 194.],
[262., 194.],
[302., 194.],
[209., 195.],
[293., 195.],
[276., 197.],
[197., 198.],
[244., 198.],
[251., 198.],
[302., 199.],
[404., 199.],
[230., 200.],
[182., 201.],
[215., 202.],
[168., 203.],
[225., 204.],
[253., 204.],
[259., 205.],
[152., 206.],
[281., 206.],
[235., 207.],
[380., 208.],
[391., 208.],
[138., 209.],
[200., 210.],
[126., 211.],
[186., 213.],
[218., 213.],
[229., 213.],
[171., 215.],
[210., 215.],
[238., 215.],
[157., 219.],
[131., 220.],
[181., 220.],
[74., 221.],
[141., 221.],
[238., 221.],
[404., 221.],
[115., 222.],
[285., 224.],
[133., 226.],
[152., 226.],
[208., 227.],
[37., 228.],
[138., 228.],
[143., 228.],
[192., 230.],
[81., 231.],
[120., 233.],
[177., 233.],
[240., 234.],
[216., 238.],
[222., 238.],
[361., 238.],
[381., 238.],
[85., 239.],
[145., 240.],
[288., 240.],
[206., 241.],
[350., 242.],
[189., 244.],
[365., 244.],
[404., 245.],
[113., 246.],
[458., 247.],
[172., 248.],
[246., 248.],
[314., 249.],
[239., 250.],
[92., 251.],
[356., 251.],
[403., 252.],
[223., 253.],
[452., 253.],
[291., 254.],
[137., 255.],
[216., 255.],
[335., 256.],
[392., 256.],
[269., 257.],
[403., 257.],
[465., 257.],
[203., 258.],
[318., 260.],
[378., 260.],
[184., 262.],
[290., 263.],
[99., 264.],
[364., 264.],
[166., 266.],
[449., 266.],
[348., 267.],
[403., 267.],
[279., 268.],
[343., 268.],
[239., 269.],
[338., 269.],
[143., 271.],
[254., 272.],
[324., 273.],
[128., 274.],
[218., 274.],
[454., 274.],
[462., 274.],
[372., 277.],
[199., 278.],
[304., 278.],
[108., 279.],
[239., 282.],
[404., 282.],
[178., 283.],
[283., 283.],
[448., 285.],
[159., 287.],
[264., 287.],
[330., 288.],
[379., 288.],
[256., 289.],
[455., 289.],
[460., 290.],
[240., 293.],
[355., 294.],
[117., 295.],
[285., 296.],
[55., 298.],
[215., 299.],
[335., 299.],
[117., 300.],
[447., 300.],
[139., 302.],
[195., 303.],
[458., 303.],
[390., 304.],
[119., 305.],
[311., 305.],
[111., 306.],
[125., 306.],
[176., 308.],
[171., 309.],
[445., 310.],
[288., 311.],
[149., 314.],
[342., 315.],
[450., 316.],
[264., 317.],
[456., 317.],
[401., 319.],
[192., 320.],
[112., 323.],
[240., 323.],
[55., 324.],
[101., 325.],
[292., 326.],
[376., 326.],
[93., 327.],
[371., 327.],
[216., 329.],
[349., 333.],
[190., 335.],
[324., 340.],
[167., 341.],
[82., 342.],
[241., 343.],
[296., 347.],
[137., 348.],
[75., 352.],
[89., 353.],
[187., 353.],
[270., 354.],
[263., 356.],
[359., 356.],
[82., 358.],
[99., 359.],
[241., 362.],
[83., 363.],
[216., 368.],
[403., 368.],
[130., 369.],
[301., 369.],
[302., 374.],
[183., 377.],
[368., 378.],
[241., 387.],
[335., 388.],
[122., 393.],
[307., 396.],
[108., 397.],
[179., 401.],
[379., 403.],
[272., 406.],
[117., 408.],
[381., 408.],
[383., 413.],
[242., 415.],
[311., 416.],
[140., 419.],
[15., 421.],
[126., 422.],
[211., 423.],
[313., 424.],
[147., 425.],
[314., 429.],
[134., 433.],
[315., 434.],
[173., 435.],
[316., 439.],
[398., 441.],
[142., 444.],
[380., 447.],
[243., 450.],
[400., 455.],
[349., 457.],
[322., 466.],
[373., 470.],
[303., 471.],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan],
[np.nan, np.nan]]

KDTree(data)

Error message

Thread 1 "python" received signal SIGSEGV, Segmentation fault.
0x00007fffb862de57 in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/slamsup/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so

--------GDB backtrace--------
#0 0x00007fffb862de57 in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#1 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#2 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#3 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#4 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#5 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#6 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#7 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#8 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) ()
from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#9 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#10 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#11 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#12 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#13 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#14 0x00007fffb862e9ef in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
...
#15870 0x00007fffb862e550 in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#15871 0x00007fffb862e550 in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#15872 0x00007fffb862e550 in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#15873 0x00007fffb862e550 in build(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#15874 0x00007fffb862effe in build_ckdtree(ckdtree*, long, long, double*, double*, int, int) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#15875 0x00007fffb8619d9a in __pyx_pf_5scipy_7spatial_7ckdtree_7cKDTree_2__init__(__pyx_obj_5scipy_7spatial_7ckdtree_cKDTree*, _object*, long, _object*, _object*, _object*, _object*) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#15876 0x00007fffb861d372 in __pyx_pw_5scipy_7spatial_7ckdtree_7cKDTree_3__init__(_object*, _object*, _object*) () from /home/dasongg/miniconda3/envs/py39/lib/python3.9/site-packages/scipy/spatial/ckdtree.cpython-39-x86_64-linux-gnu.so
#15877 0x00005555556e6414 in wrap_init (kwds=0x0, wrapped=<optimized out>, args=<optimized out>, self=0x7fffb6d34040) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/typeobject.c:6189
#15878 wrapperdescr_raw_call.isra.8 (kwds=0x0, args=<optimized out>, self=0x7fffb6d34040) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/descrobject.c:510
#15879 wrapper_call (wp=<optimized out>, args=<optimized out>, kwds=0x0) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/descrobject.c:1384
#15880 0x000055555568fcaf in _PyObject_MakeTpCall (tstate=0x55555591eae0, callable=0x7ffff73f3640, args=<optimized out>, nargs=<optimized out>, keywords=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/call.c:191
#15881 0x000055555572dd89 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x7fffb7184bf8, callable=<optimized out>, tstate=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Include/cpython/abstract.h:116
#15882 PyObject_Vectorcall () at /opt/conda/conda-bld/python-split_1649141344976/work/Include/cpython/abstract.h:127
#15883 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=0x55555591eae0) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:5077
#15884 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=0x7fffb7184a40, throwflag=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:3489
#15885 0x00005555556ea663 in _PyEval_EvalFrame () at /opt/conda/conda-bld/python-split_1649141344976/work/Include/internal/pycore_ceval.h:40
#15886 _PyEval_EvalCode (tstate=<optimized out>, _co=<optimized out>, globals=<optimized out>, locals=<optimized out>, args=<optimized out>, argcount=<optimized out>, kwnames=0x0, kwargs=0x7fffffffd900, kwcount=<optimized out>, kwstep=1, defs=0x7fffb86a50a8, defcount=5, kwdefs=0x0, closure=0x7fffb86a98e0, name=<optimized out>, qualname=0x7fffb86a4130) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:4329
#15887 0x00005555556eb354 in _PyFunction_Vectorcall (func=<optimized out>, stack=0x7fffffffd8f0, nargsf=<optimized out>, kwnames=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/call.c:396
#15888 0x00005555556d5eca in _PyObject_FastCallDictTstate (tstate=0x55555591eae0, callable=0x7fffb7daea60, args=<optimized out>, nargsf=<optimized out>, kwargs=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/call.c:118
#15889 0x00005555556dfb79 in _PyObject_Call_Prepend (kwargs=0x0, args=0x7ffff75f8a60, obj=<optimized out>, callable=0x7fffb7daea60, tstate=0x55555591eae0) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/call.c:489
#15890 slot_tp_init (self=<optimized out>, args=0x7ffff75f8a60, kwds=0x0) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/typeobject.c:6969
#15891 0x000055555568fd5f in type_call (kwds=0x0, args=0x7ffff75f8a60, type=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/typeobject.c:1026
#15892 _PyObject_MakeTpCall (tstate=0x55555591eae0, callable=0x555555e64660, args=<optimized out>, nargs=<optimized out>, keywords=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Objects/call.c:191
#15893 0x000055555572dd89 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x555555a23238, callable=<optimized out>, tstate=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Include/cpython/abstract.h:116
#15894 PyObject_Vectorcall () at /opt/conda/conda-bld/python-split_1649141344976/work/Include/cpython/abstract.h:127
#15895 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=0x55555591eae0) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:5077
#15896 _PyEval_EvalFrameDefault (tstate=<optimized out>, f=0x555555a230c0, throwflag=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:3489
#15897 0x00005555556ea663 in _PyEval_EvalFrame () at /opt/conda/conda-bld/python-split_1649141344976/work/Include/internal/pycore_ceval.h:40
#15898 _PyEval_EvalCode (tstate=<optimized out>, _co=<optimized out>, globals=<optimized out>, locals=<optimized out>, args=<optimized out>, argcount=<optimized out>, kwnames=0x0, kwargs=0x0, kwcount=<optimized out>, kwstep=2, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0, name=<optimized out>, qualname=0x0) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:4329
#15899 0x000055555579745c in _PyEval_EvalCodeWithName (qualname=0x0, name=0x0, closure=0x0, kwdefs=0x0, defcount=0, defs=0x0, kwstep=2, kwcount=0, kwargs=<optimized out>, kwnames=<optimized out>, argcount=<optimized out>, args=<optimized out>, locals=<optimized out>, globals=<optimized out>, _co=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:4361
#15900 PyEval_EvalCodeEx (_co=<optimized out>, globals=<optimized out>, locals=<optimized out>, args=<optimized out>, argcount=<optimized out>, kws=<optimized out>, kwcount=0, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:4377
#15901 0x00005555556eb45b in PyEval_EvalCode (co=<optimized out>, globals=<optimized out>, locals=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/ceval.c:828
#15902 0x000055555579750b in run_eval_code_obj (tstate=0x55555591eae0, co=0x7ffff7607870, globals=0x7ffff75fae80, locals=0x7ffff75fae80) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/pythonrun.c:1221
#15903 0x00005555557c7f75 in run_mod (mod=<optimized out>, filename=<optimized out>, globals=0x7ffff75fae80, locals=0x7ffff75fae80, flags=<optimized out>, arena=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/pythonrun.c:1242
#15904 0x0000555555668987 in pyrun_file (fp=0x55555591e700, filename=0x7ffff7532330, start=<optimized out>, globals=0x7ffff75fae80, locals=0x7ffff75fae80, closeit=1, flags=0x7fffffffdd68) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/pythonrun.c:1140
#15905 0x00005555557cda2f in pyrun_simple_file (flags=0x7fffffffdd68, closeit=1, filename=0x7ffff7532330, fp=0x55555591e700) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/pythonrun.c:450
#15906 PyRun_SimpleFileExFlags (fp=0x55555591e700, filename=<optimized out>, closeit=1, flags=0x7fffffffdd68) at /opt/conda/conda-bld/python-split_1649141344976/work/Python/pythonrun.c:483
#15907 0x00005555557ce10b in pymain_run_file (cf=0x7fffffffdd68, config=0x55555591f650) at /opt/conda/conda-bld/python-split_1649141344976/work/Modules/main.c:379
#15908 pymain_run_python (exitcode=0x7fffffffdd60) at /opt/conda/conda-bld/python-split_1649141344976/work/Modules/main.c:604
#15909 Py_RunMain () at /opt/conda/conda-bld/python-split_1649141344976/work/Modules/main.c:683
#15910 0x00005555557ce309 in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at /opt/conda/conda-bld/python-split_1649141344976/work/Modules/main.c:1129
#15911 0x00007ffff7c62083 in __libc_start_main (main=0x5555556754a0 <main>, argc=2, argv=0x7fffffffdf68, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffdf58) at ../csu/libc-start.c:308
#15912 0x00005555557550a0 in _start ()

SciPy/NumPy/Python version information

1.7.3 1.21.2 sys.version_info(major=3, minor=9, micro=12, releaselevel='final', serial=0)

Issue 2:
When running the code below (boiled down from the original producer) I get a segmentation fault.

Reproducing code example:

import numpy as np
from scipy.spatial import KDTree

points = np.arange(34.).reshape(-1, 2)
points[0] = np.nan
points[8] = np.nan
KDTree(points)

Error message:

❯ python test_crash.py
[1] 89987 segmentation fault python test_crash.py

Scipy/Numpy/Python version information:

1.7.1 1.21.1 sys.version_info(major=3, minor=9, micro=6, releaselevel='final', serial=0)

I'm running this locally on macOS 11.5 (x86_64), but original issue was seen on Ubuntu 20.04.2 (GitHub Actions). The MetPy test that now segfaults passes fine with Scipy 1.7.0.