Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
3.9.0 (under development)
3.8.1 (under development)
=========================

- Fix a bug in `restore_original_limits()` where all limits were always
restored, even if only some were modified. Similarly,
`get_original_num_threads()` will only return original values for those values
that have been modified.
https://github.com/joblib/threadpoolctl/pull/309

3.8.0 (2026-11-05)
==================

Expand Down
51 changes: 51 additions & 0 deletions tests/test_threadpoolctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1640,3 +1640,54 @@ def test_linux_does_not_import_ctypes_util():
assert "ctypes.util" not in sys.modules
"""
subprocess.check_call([sys.executable, "-c", script], env=env)


@pytest.mark.parametrize(["api1", "api2"], [("blas", "openmp"), ("openmp", "blas")])
def test_restore_only_restores_modified_limits(api1, api2, request):
try:
lib1 = ThreadpoolController().select(user_api=api1).lib_controllers[0]
orig_limit1 = lib1.get_num_threads()
lib2 = ThreadpoolController().select(user_api=api2).lib_controllers[0]
orig_limit2 = lib2.get_num_threads()
except IndexError:
pytest.skip("need both BLAS and OpenMP")
if orig_limit1 < 3 or orig_limit2 < 3:
pytest.skip("need at least 3 cores")

restore_limiter = threadpool_limits(limits=None)

def done():
restore_limiter.restore_original_limits()

request.addfinalizer(done)

limiter1 = threadpool_limits(1, user_api=api1)
assert limiter1.get_original_num_threads() == {api1: orig_limit1}
limiter2 = threadpool_limits(2, user_api=api2)
assert limiter2.get_original_num_threads() == {api2: orig_limit2}

# This should only overwrite limits for api1, not api2:
limiter1.restore_original_limits()
assert lib1.get_num_threads() == orig_limit1
assert lib2.get_num_threads() == 2

# This should only overwrite limits for api2, not api1:
limiter2.restore_original_limits()
assert lib1.get_num_threads() == orig_limit1
assert lib2.get_num_threads() == orig_limit2

# limit=None should restore everthing:
limiter_all = threadpool_limits(limits=None)
assert limiter_all.get_original_num_threads() == {
api1: orig_limit1,
api2: orig_limit2,
}
assert lib1.get_num_threads() == orig_limit1
assert lib2.get_num_threads() == orig_limit2
threadpool_limits(1, user_api=api1)
threadpool_limits(2, user_api=api2)
assert lib1.get_num_threads() == 1
assert lib2.get_num_threads() == 2
limiter_all.restore_original_limits()
assert lib1.get_num_threads() == orig_limit1
assert lib2.get_num_threads() == orig_limit2
13 changes: 11 additions & 2 deletions threadpoolctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,9 @@ class _ThreadpoolLimiter:
"""

def __init__(self, controller, *, limits=None, user_api=None):
if user_api is not None:
# Ensure we only restore the libraries we actually modified:
controller = controller.select(user_api=user_api)
self._controller = controller
self._limits, self._user_api = _check_limiter_params(
controller, limits, user_api
Expand Down Expand Up @@ -1275,7 +1278,10 @@ class threadpool_limits(_ThreadpoolLimiter):
calls within an OpenMP parallel region. The `user_api` parameter is
ignored.

- If None, this function does not do anything.
- If None, this function does not do anything. The returned limiter
object acts as if all libraries were set to the current limit, so
you can use its `restore_original_limits()` method to restore all
libraries to their limit at the time this method was called.

user_api : {USER_APIS} or None (default=None)
APIs of libraries to limit. Used only if `limits` is an int.
Expand Down Expand Up @@ -1441,7 +1447,10 @@ def limit(self, *, limits=None, user_api=None):
calls within an OpenMP parallel region. The `user_api` parameter is
ignored.

- If None, this function does not do anything.
- If None, this function does not do anything. The returned limiter
object acts as if all libraries were set to the current limit, so
you can use its `restore_original_limits()` method to restore all
libraries to their limit at the time this method was called.

user_api : {USER_APIS} or None (default=None)
APIs of libraries to limit. Used only if `limits` is an int.
Expand Down
Loading