diff --git a/CHANGES.md b/CHANGES.md index eb539e0d..12cb06f6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) ================== diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index 93c32292..cb353b69 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -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 diff --git a/threadpoolctl.py b/threadpoolctl.py index 40377c03..a9b6bdee 100644 --- a/threadpoolctl.py +++ b/threadpoolctl.py @@ -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 @@ -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. @@ -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.