Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

`threadpool_limits` is not thread-safe under `ThreadPoolExecutor`

オープン
#205 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
1〜2日
初心者へのやさしさ
48/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
numpy, python, pytorch

調査の方向性

Start by reading the threadpool_limits context manager and its tests; the issue does not name their file paths. Reproduce the reported behavior with the supplied ThreadPoolExecutor example and inspect how limits are changed and restored across overlapping contexts. Done means concurrent workers consistently observe the intended limit and the original limits are restored afterward.

索引モデルが issue の本文から書いたものです。

説明

When using threadpoolctl.threadpool_limits inside multiple Python threads (e.g. via ThreadPoolExecutor), the thread limit is not reliably enforced.
Despite using with threadpool_limits(limits=2) inside each worker, some threads observe OpenBLAS using the full default thread count.

Reproducible Example

#!/usr/bin/env python3
from threadpoolctl import threadpool_limits, threadpool_info
import torch

from concurrent.futures import ThreadPoolExecutor, as_completed


def worker(i):
    # NOTE: this appears to be process-global, not thread-local
    with threadpool_limits(limits=2):
        a = torch.randn(512, 256, device="cpu")
        b = torch.randn(256, 512, device="cpu")
        _ = a @ b
        info = threadpool_info()
        print(f"[worker {i}] threadpool_info:", info)
        return info


results = []

with ThreadPoolExecutor(max_workers=8) as executor:
    futures = [executor.submit(worker, i) for i in range(8)]
    for fut in as_completed(futures):
        results.append(fut.result())

print("final threadpool_info:")
print(threadpool_info())

Actual Output

[worker 6] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
[worker 1] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
[worker 5] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 24, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
[worker 3] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
[worker 4] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
[worker 0] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
[worker 7] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
[worker 2] threadpool_info: [{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 2, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]
final threadpool_info:
[{'user_api': 'blas', 'internal_api': 'openblas', 'num_threads': 2, 'prefix': 'libscipy_openblas', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/numpy.libs/libscipy_openblas64_-56d6093b.so', 'version': '0.3.29', 'threading_layer': 'pthreads', 'architecture': 'Haswell'}, {'user_api': 'openmp', 'internal_api': 'openmp', 'num_threads': 24, 'prefix': 'libgomp', 'filepath': '/root/miniconda3/envs/gp_py311/lib/python3.11/site-packages/torch/lib/libgomp.so.1', 'version': None}]

Some workers correctly observe num_threads = 2, while others observe num_threads = 24, even though all are wrapped in with threadpool_limits(limits=2).


Environment

Distributor ID: Ubuntu
Description: Ubuntu 22.04.5 LTS
Release: 22.04
Codename: jammy

torch: 2.9.0
numpy: 2.2.6
scipy: 1.16.2
threadpoolctl: 3.6.0

主要言語
Python
スター
434
フォーク
38
平均マージ
2日 14時間
マージ済み PR(30日)
8

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

joblib/threadpoolctl のほかの issue

joblib/threadpoolctl の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。