Skip to content

FIX Reference-count BLAS thread limit in pairwise distance reductions - #34878

Closed
VHarshedh wants to merge 1 commit into
scikit-learn:mainfrom
VHarshedh:fix-openblas-openmp-reentrancy
Closed

VHarshedh wants to merge 1 commit into
scikit-learn:mainfrom
VHarshedh:fix-openblas-openmp-reentrancy

Conversation

@VHarshedh

Copy link
Copy Markdown

Reference Issues/PRs

Fixes #31884

What does this implement/fix? Explain your changes.

When calling ArgKmin64.compute() or RadiusNeighbors64.compute() concurrently across threads (e.g. using joblib.Parallel(backend="threading")), numerical corruption occurs in environments running OpenBLAS compiled with the OpenMP threading layer.

The root cause is overlapping threadpoolctl context manager lifecycles: threadpoolctl changes process-global C state (openblas_set_num_threads). When Thread A exits its compute() block, it prematurely restores the thread limit back to default (e.g. 12) while concurrent threads are still running _gemm operations, corrupting OpenBLAS's non-reentrant static packing buffers.

This PR introduces _RefcountedBLASLimit in sklearn.utils.parallel. It reference-counts concurrent entries across threads so the BLAS thread limit stays pinned to 1 as long as any reduction is active, and only restores the original thread count once the last concurrent thread exits.

Any other comments?

  • Added unit tests for _RefcountedBLASLimit in test_parallel.py.
  • Added a multithreaded regression test in test_pairwise_distances_reduction.py.
  • Included a towncrier news entry.

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Thank you for opening your first pull request to scikit-learn! 🎉

To help get your contribution reviewed, please make sure that:

  • You have filled out the pull request template.

  • The pull request addresses an existing issue that is ready for contribution (e.g. not tagged as 'Needs Triage', 'Needs Decision', ...). If you are proposing a new feature, please open an issue to discuss it first.

  • There are no other open pull requests already targeting the same issue.

  • You have followed the pull request checklist. In particular, linting and tests should pass.

@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Sep 3, 2026
@VHarshedh
VHarshedh force-pushed the fix-openblas-openmp-reentrancy branch from 12d9098 to 52aba9d Compare September 3, 2026 11:19
Fixes a race condition and silent data corruption when ArgKmin or
RadiusNeighbors are invoked concurrently across threads on OpenBLAS
built with OpenMP. Overlapping threadpoolctl context manager lifecycles
prematurely restored global BLAS thread counts while concurrent GEMM
operations were still running. We introduce _RefcountedBLASLimit to
ensure the limit remains pegged at 1 until all concurrent tasks exit.

Closes scikit-learn#31884
@VHarshedh
VHarshedh force-pushed the fix-openblas-openmp-reentrancy branch from 52aba9d to 16cc7f9 Compare September 3, 2026 11:29
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Sep 3, 2026
@itamarst

Copy link
Copy Markdown
Contributor

Thank you!

I think however an easier solution is to upgrade threadpoolctl to the latest version (3.7 or later), which makes the thread pool size setting API thread local. See discussion in the original issue.

@cakedev0

Copy link
Copy Markdown
Member

Thank you for your work but threadpoolctl 3.7 fixes the issue and is now the default version people get when installing scikit-learn (including very old versions like 1.2), so we're not going to make the effort to review and maintain this fix, sorry.

@cakedev0 cakedev0 closed this Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pairwise_distances_argmin_min / ArgKMin64 is not thread-safe

3 participants