V1.5 randomly hangs on import

### Describe the bug

Since upgrading to version 1.5.0 we are seeing issues where the process stalls on import. Through a core dump, it appears to be locking up [here](https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/__init__.py#L154). We run hundreds of processes in parallel and it will hang on only a handful (less than 10).

### Steps/Code to Reproduce

```
import sklearn
```

### Expected Results

it imports

### Actual Results

process hangs indefinitely.

### Versions

```shell
System:
    python: 3.11.3 (main, May 15 2023, 15:45:52) [GCC 11.2.0]
executable: /home/jcoder/git/neo/neo/pyenv/bin/python
   machine: Linux-6.1.87-x86_64-with-glibc2.31

Python dependencies:
      sklearn: 1.5.0
          pip: 24.0
   setuptools: 70.0.0
        numpy: 1.24.4
        scipy: 1.11.1
       Cython: 0.29.35
       pandas: 2.0.2
   matplotlib: 3.7.2
       joblib: 1.3.0
threadpoolctl: 3.1.0

Built with OpenMP: True

threadpoolctl info:
       user_api: blas
   internal_api: openblas
         prefix: libopenblas
       filepath: /opt/anaconda/2023/envs/pandas2/lib/libopenblasp-r0.3.23.so
        version: 0.3.23
threading_layer: pthreads
   architecture: Zen
    num_threads: 64

       user_api: openmp
   internal_api: openmp
         prefix: libgomp
       filepath: /home/~~~~~~/lib/python3.11/site-packages/scikit_learn.libs/libgomp-a34b3233.so.1.0.0
        version: None
    num_threads: 64
```

COMMENT:
I got this error while doing `pip install git+https://github.com/jeremiedbb/scikit-learn@lazy-threadpool-controller `Here is the error : 
```
C:\Windows\System32>pip install git+https://github.com/jeremiedbb/scikit-learn@lazy-threadpool-controller
Collecting git+https://github.com/jeremiedbb/scikit-learn@lazy-threadpool-controller
  Cloning https://github.com/jeremiedbb/scikit-learn (to revision lazy-threadpool-controller) to c:\users\victim\appdata\local\temp\pip-req-build-52a6wbzb
  Running command git clone --filter=blob:none --quiet https://github.com/jeremiedbb/scikit-learn 'C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb'
  Running command git checkout -b lazy-threadpool-controller --track origin/lazy-threadpool-controller
  branch 'lazy-threadpool-controller' set up to track 'origin/lazy-threadpool-controller'.
  Switched to a new branch 'lazy-threadpool-controller'
  Resolved https://github.com/jeremiedbb/scikit-learn to commit 0f42bd1c989e61613f27658720f401ab8f8f6996
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Preparing metadata (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [166 lines of output]
      + meson setup C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb\.mesonpy-6exwddi1 -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --native-file=C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb\.mesonpy-6exwddi1\meson-python-native-file.ini
      The Meson build system
      Version: 1.5.1
      Source dir: C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb
      Build dir: C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb\.mesonpy-6exwddi1
      Build type: native build
      Project name: scikit-learn
      Project version: 1.6.dev0
      Activating VS 17.11.2
      C compiler for the host machine: cl (msvc 19.41.34120 "Microsoft (R) C/C++ Optimizing Compiler Version 19.41.34120 for x64")
      C linker for the host machine: link link 14.41.34120.0
      C++ compiler for the host machine: cl (msvc 19.41.34120 "Microsoft (R) C/C++ Optimizing Compiler Version 19.41.34120 for x64")
      C++ linker for the host machine: link link 14.41.34120.0
      Cython compiler for the host machine: cython (cython 3.0.11)
      Host machine cpu family: x86_64
      Host machine cpu: x86_64
      Compiler for C supports arguments -Wno-unused-but-set-variable: NO
      Compiler for C supports arguments -Wno-unused-function: NO
      Compiler for C supports arguments -Wno-conversion: NO
      Compiler for C supports arguments -Wno-misleading-indentation: NO
      Library m found: NO
      Program python found: YES (C:\Users\victim\AppData\Local\Programs\Python\Python312\python.exe)
      Run-time dependency OpenMP for c found: YES 2.0
      Run-time dependency python found: YES 3.12
      Build targets in project: 111

      scikit-learn 1.6.dev0

        User defined options
          Native files: C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb\.mesonpy-6exwddi1\meson-python-native-file.ini
          buildtype   : release
          b_ndebug    : if-release
          b_vscrt     : md

      Found ninja.EXE-1.11.1.git.kitware.jobserver-1 at C:\Users\victim\AppData\Local\Temp\pip-build-env-glvhzd87\normal\Scripts\ninja.EXE
      + meson compile
      [1/249] Generating sklearn/write_built_with_meson_file with a custom command
      [2/249] Generating sklearn/utils/_seq_dataset_pxd with a custom command
      [3/249] Generating sklearn/utils/_weight_vector_pxd with a custom command
      [4/249] Generating sklearn/metrics/_dist_metrics_pxd with a custom command
      [5/249] Copying file sklearn/__init__.py
      [6/249] Generating sklearn/metrics/_pairwise_distances_reduction/_datasets_pair_pxd with a custom command
      [7/249] Generating sklearn/metrics/_pairwise_distances_reduction/_base_pxd with a custom command
      [8/249] Copying file sklearn/utils/_cython_blas.pxd
      [9/249] Copying file sklearn/utils/_vector_sentinel.pxd
      [10/249] Generating sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer_pxd with a custom command
      [11/249] Copying file sklearn/utils/_heap.pxd
      [12/249] Copying file sklearn/utils/_openmp_helpers.pxd
      [13/249] Copying file sklearn/_loss/_loss.pxd
      [14/249] Copying file sklearn/utils/_typedefs.pxd
      [15/249] Generating sklearn/metrics/_pairwise_distances_reduction/_argkmin_pxd with a custom command
      [16/249] Copying file sklearn/utils/_random.pxd
      [17/249] Copying file sklearn/metrics/__init__.py
      [18/249] Generating sklearn/metrics/_pairwise_distances_reduction/_radius_neighbors_pxd with a custom command
      [19/249] Copying file sklearn/utils/__init__.py
      [20/249] Copying file sklearn/metrics/_pairwise_distances_reduction/_classmode.pxd
      [21/249] Copying file sklearn/metrics/_pairwise_distances_reduction/__init__.py
      [22/249] Copying file sklearn/utils/_sorting.pxd
      [23/249] Generating sklearn/_loss/_loss_pyx with a custom command
      [24/249] Copying file sklearn/linear_model/__init__.py
      [25/249] Copying file sklearn/linear_model/_sgd_fast.pxd
      [26/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/__check_build/_check_build.pyx
      [27/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/ensemble/_hist_gradient_boosting/common.pyx
      [28/249] Compiling C object sklearn/__check_build/_check_build.cp312-win_amd64.pyd.p/meson-generated_sklearn___check_build__check_build.pyx.c.obj
      [29/249] Linking target sklearn/__check_build/_check_build.cp312-win_amd64.pyd
         Creating library sklearn\__check_build\_check_build.cp312-win_amd64.lib and object sklearn\__check_build\_check_build.cp312-win_amd64.exp

      [30/249] Compiling C++ object sklearn/utils/murmurhash.cp312-win_amd64.pyd.p/src_MurmurHash3.cpp.obj
      [31/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/ensemble/_hist_gradient_boosting/_gradient_boosting.pyx
      [32/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/metrics/cluster/_expected_mutual_info_fast.pyx
      [33/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/ensemble/_hist_gradient_boosting/_bitset.pyx
      [34/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/ensemble/_hist_gradient_boosting/_predictor.pyx
      [35/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/ensemble/_hist_gradient_boosting/_binning.pyx
      [36/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/ensemble/_hist_gradient_boosting/histogram.pyx
      [37/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/ensemble/_hist_gradient_boosting/splitting.pyx
      [38/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/cluster/_hdbscan/_tree.pyx
      [39/249] Generating sklearn/utils/_seq_dataset_pyx with a custom command
      [40/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_openmp_helpers.pyx
      [41/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/murmurhash.pyx
      [42/249] Compiling C object sklearn/utils/_openmp_helpers.cp312-win_amd64.pyd.p/meson-generated_sklearn_utils__openmp_helpers.pyx.c.obj
      [43/249] Linking target sklearn/utils/_openmp_helpers.cp312-win_amd64.pyd
         Creating library sklearn\utils\_openmp_helpers.cp312-win_amd64.lib and object sklearn\utils\_openmp_helpers.cp312-win_amd64.exp
      [44/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_fast_dict.pyx
      [45/249] Generating sklearn/utils/_weight_vector_pyx with a custom command
      [46/249] Compiling C object sklearn/utils/murmurhash.cp312-win_amd64.pyd.p/meson-generated_sklearn_utils_murmurhash.pyx.c.obj
      ..\sklearn\utils\src/MurmurHash3.h(16): warning C4142: 'uint32_t': benign redefinition of type
      C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.41.34120\include\stdint.h(24): note: see declaration of 'uint32_t'
      [47/249] Linking target sklearn/utils/murmurhash.cp312-win_amd64.pyd
         Creating library sklearn\utils\murmurhash.cp312-win_amd64.lib and object sklearn\utils\murmurhash.cp312-win_amd64.exp

      [48/249] Generating sklearn/metrics/_dist_metrics_pyx with a custom command
      [49/249] Compiling Cython source sklearn/utils/_seq_dataset.pyx
      [50/249] Compiling Cython source sklearn/utils/_weight_vector.pyx
      [51/249] Compiling C++ object sklearn/utils/_fast_dict.cp312-win_amd64.pyd.p/meson-generated_sklearn_utils__fast_dict.pyx.cpp.obj
      sklearn/utils/_fast_dict.cp312-win_amd64.pyd.p/sklearn/utils/_fast_dict.pyx.cpp(26950): warning C4551: function call missing argument list
      sklearn/utils/_fast_dict.cp312-win_amd64.pyd.p/sklearn/utils/_fast_dict.pyx.cpp(30800): warning C4551: function call missing argument list
      sklearn/utils/_fast_dict.cp312-win_amd64.pyd.p/sklearn/utils/_fast_dict.pyx.cpp(30801): warning C4551: function call missing argument list
      sklearn/utils/_fast_dict.cp312-win_amd64.pyd.p/sklearn/utils/_fast_dict.pyx.cpp(30802): warning C4551: function call missing argument list
      [52/249] Linking target sklearn/utils/_fast_dict.cp312-win_amd64.pyd
         Creating library sklearn\utils\_fast_dict.cp312-win_amd64.lib and object sklearn\utils\_fast_dict.cp312-win_amd64.exp

      [53/249] Compiling C object sklearn/utils/_seq_dataset.cp312-win_amd64.pyd.p/meson-generated_sklearn_utils__seq_dataset.pyx.c.obj
      [54/249] Linking target sklearn/utils/_seq_dataset.cp312-win_amd64.pyd
         Creating library sklearn\utils\_seq_dataset.cp312-win_amd64.lib and object sklearn\utils\_seq_dataset.cp312-win_amd64.exp
      [55/249] Compiling C object sklearn/utils/_weight_vector.cp312-win_amd64.pyd.p/meson-generated_sklearn_utils__weight_vector.pyx.c.obj
      [56/249] Linking target sklearn/utils/_weight_vector.cp312-win_amd64.pyd
         Creating library sklearn\utils\_weight_vector.cp312-win_amd64.lib and object sklearn\utils\_weight_vector.cp312-win_amd64.exp
      [57/249] Generating sklearn/metrics/_pairwise_distances_reduction/_datasets_pair_pyx with a custom command
      [58/249] Generating sklearn/metrics/_pairwise_distances_reduction/_base_pyx with a custom command
      [59/249] Compiling Cython source sklearn/metrics/_dist_metrics.pyx
      warning: sklearn\metrics\_dist_metrics.pyx:846:44: Assigning to 'float64_t *' from 'const float64_t *' discards const qualifier
      warning: sklearn\metrics\_dist_metrics.pyx:909:40: Assigning to 'float64_t *' from 'const float64_t *' discards const qualifier

      warning: sklearn\metrics\_dist_metrics.pyx:3426:44: Assigning to 'float32_t *' from 'const float32_t *' discards const qualifier

      warning: sklearn\metrics\_dist_metrics.pyx:3489:40: Assigning to 'float32_t *' from 'const float32_t *' discards const qualifier

      [60/249] Compiling Cython source sklearn/metrics/_pairwise_distances_reduction/_base.pyx
      [61/249] Compiling Cython source sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.pyx
      [62/249] Compiling C++ object sklearn/metrics/_pairwise_distances_reduction/_base.cp312-win_amd64.pyd.p/meson-generated_sklearn_metrics__pairwise_distances_reduction__base.pyx.cpp.obj
      sklearn/metrics/_pairwise_distances_reduction/_base.cp312-win_amd64.pyd.p/sklearn/metrics/_pairwise_distances_reduction/_base.pyx.cpp(32606): warning C4551: function call missing argument list
      [63/249] Compiling C++ object sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.cp312-win_amd64.pyd.p/meson-generated_sklearn_metrics__pairwise_distances_reduction__datasets_pair.pyx.cpp.obj
      sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.cp312-win_amd64.pyd.p/sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.pyx.cpp(43425): warning C4551: function call missing argument list
      [64/249] Linking target sklearn/metrics/_pairwise_distances_reduction/_base.cp312-win_amd64.pyd
         Creating library sklearn\metrics\_pairwise_distances_reduction\_base.cp312-win_amd64.lib and object sklearn\metrics\_pairwise_distances_reduction\_base.cp312-win_amd64.exp

      [65/249] Linking target sklearn/metrics/_pairwise_distances_reduction/_datasets_pair.cp312-win_amd64.pyd
         Creating library sklearn\metrics\_pairwise_distances_reduction\_datasets_pair.cp312-win_amd64.lib and object sklearn\metrics\_pairwise_distances_reduction\_datasets_pair.cp312-win_amd64.exp

      [66/249] Generating sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer_pyx with a custom command
      [67/249] Generating sklearn/metrics/_pairwise_distances_reduction/_argkmin_pyx with a custom command
      [68/249] Compiling Cython source sklearn/metrics/_pairwise_distances_reduction/_argkmin.pyx
      [69/249] Compiling Cython source sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.pyx
      [70/249] Compiling C++ object sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.cp312-win_amd64.pyd.p/meson-generated_sklearn_metrics__pairwise_distances_reduction__middle_term_computer.pyx.cpp.obj
      FAILED: sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.cp312-win_amd64.pyd.p/meson-generated_sklearn_metrics__pairwise_distances_reduction__middle_term_computer.pyx.cpp.obj
      "cl" "-Isklearn\metrics\_pairwise_distances_reduction\_middle_term_computer.cp312-win_amd64.pyd.p" "-Isklearn\metrics\_pairwise_distances_reduction" "-I..\sklearn\metrics\_pairwise_distances_reduction" "-I..\..\pip-build-env-glvhzd87\overlay\Lib\site-packages\numpy\_core\include" "-IC:\Users\victim\AppData\Local\Programs\Python\Python312\Include" "-DNDEBUG" "/MD" "/nologo" "/showIncludes" "/utf-8" "/Zc:__cplusplus" "/W2" "/EHsc" "/std:c++14" "/permissive-" "/O2" "/Gw" "/openmp" "/Fdsklearn\metrics\_pairwise_distances_reduction\_middle_term_computer.cp312-win_amd64.pyd.p\meson-generated_sklearn_metrics__pairwise_distances_reduction__middle_term_computer.pyx.cpp.pdb" /Fosklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.cp312-win_amd64.pyd.p/meson-generated_sklearn_metrics__pairwise_distances_reduction__middle_term_computer.pyx.cpp.obj "/c" sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.cp312-win_amd64.pyd.p/sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.pyx.cpp
      sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.cp312-win_amd64.pyd.p/sklearn/metrics/_pairwise_distances_reduction/_middle_term_computer.pyx.cpp(42045): warning C4551: function call missing argument list
      C:\Users\victim\AppData\Local\Temp\pip-req-build-52a6wbzb\.mesonpy-6exwddi1\sklearn\metrics\_pairwise_distances_reduction\_middle_term_computer.cp312-win_amd64.pyd.p\sklearn\metrics\_pairwise_distances_reduction\_middle_term_computer.pyx.cpp : fatal error C1083: Cannot open compiler generated file: '': Invalid argument
      [71/249] Compiling C object sklearn/metrics/_dist_metrics.cp312-win_amd64.pyd.p/meson-generated_sklearn_metrics__dist_metrics.pyx.c.obj
      C:\Users\victim\AppData\Local\Temp\pip-build-env-glvhzd87\overlay\Lib\site-packages\numpy\_core\include\numpy\npy_1_7_deprecated_api.h(14) : Warning Msg: Using deprecated NumPy API, disable it with #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
      sklearn/metrics/_dist_metrics.cp312-win_amd64.pyd.p/sklearn/metrics/_dist_metrics.pyx.c(29538): warning C4090: '=': different 'const' qualifiers
      sklearn/metrics/_dist_metrics.cp312-win_amd64.pyd.p/sklearn/metrics/_dist_metrics.pyx.c(30323): warning C4090: '=': different 'const' qualifiers
      sklearn/metrics/_dist_metrics.cp312-win_amd64.pyd.p/sklearn/metrics/_dist_metrics.pyx.c(49162): warning C4090: '=': different 'const' qualifiers
      sklearn/metrics/_dist_metrics.cp312-win_amd64.pyd.p/sklearn/metrics/_dist_metrics.pyx.c(49947): warning C4090: '=': different 'const' qualifiers
      [72/249] Compiling C++ object sklearn/metrics/_pairwise_distances_reduction/_argkmin.cp312-win_amd64.pyd.p/meson-generated_sklearn_metrics__pairwise_distances_reduction__argkmin.pyx.cpp.obj
      sklearn/metrics/_pairwise_distances_reduction/_argkmin.cp312-win_amd64.pyd.p/sklearn/metrics/_pairwise_distances_reduction/_argkmin.pyx.cpp(35870): warning C4551: function call missing argument list
      [73/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_sorting.pyx
      [74/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_heap.pyx      [75/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_vector_sentinel.pyx
      [76/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/decomposition/_cdnmf_fast.pyx
      [77/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_isfinite.pyx
      [78/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/metrics/_pairwise_fast.pyx
      [79/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/_isotonic.pyx
      [80/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_typedefs.pyx
      [81/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/cluster/_hdbscan/_reachability.pyx
      [82/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/arrayfuncs.pyx
      [83/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_random.pyx
      [84/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/_cython_blas.pyx
      [85/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/datasets/_svmlight_format_fast.pyx
      [86/249] Compiling Cython source C:/Users/victim/AppData/Local/Temp/pip-req-build-52a6wbzb/sklearn/utils/sparsefuncs_fast.pyx
      [87/249] Compiling Cython source sklearn/_loss/_loss.pyx
      ninja: build stopped: subcommand failed.
      Activating VS 17.11.2
      INFO: automatically activated MSVC compiler environment
      INFO: autodetecting backend as ninja
      INFO: calculating backend command to run: C:\Users\victim\AppData\Local\Temp\pip-build-env-glvhzd87\normal\Scripts\ninja.EXE
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details
```

Labels: Bug, Needs Reproducible Code