From 9b1d9a95ec9bc4a04c858653db8e846dca5f4281 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Wed, 18 Nov 2026 10:34:51 -0500 Subject: [PATCH 1/4] Reproducer for the problem where restoration is over-aggressive --- tests/test_threadpoolctl.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index 93c32292..3e4d6afd 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -1640,3 +1640,40 @@ 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(): + # limits=None should restore everything: + restore_limiter.restore_original_limits() + assert lib1.get_num_threads() == orig_limit1 + assert lib2.get_num_threads() == orig_limit2 + + request.addfinalizer(done) + + limiter1 = threadpool_limits(1, user_api=api1) + limiter2 = threadpool_limits(2, user_api=api2) + + # 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 + # The limit for api2 was 1 when limiter2 was created, so that's what it + # should restore to: + assert lib2.get_num_threads() == 1 From ca03724d30a2760e73ea44c4a03a12863fc24dba Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Wed, 18 Nov 2026 11:20:50 -0500 Subject: [PATCH 2/4] Changelog --- CHANGES.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index eb539e0d..0f6d4535 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,10 @@ -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. + https://github.com/joblib/threadpoolctl/pull/309 + 3.8.0 (2026-11-05) ================== From d80f87735defc06656e6b607f0e637280a68379b Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Wed, 18 Nov 2026 11:40:28 -0500 Subject: [PATCH 3/4] Make limiter returned by limit() only restore modified user APIs --- CHANGES.md | 4 +++- tests/test_threadpoolctl.py | 20 ++++++++++++++------ threadpoolctl.py | 13 +++++++++++-- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 0f6d4535..12cb06f6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,9 @@ ========================= - Fix a bug in `restore_original_limits()` where all limits were always - restored, even if only some were modified. + 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 3e4d6afd..2cdbdf2c 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -1655,11 +1655,9 @@ def test_restore_only_restores_modified_limits(api1, api2, request): pytest.skip("need at least 3 cores") restore_limiter = threadpool_limits(limits=None) + def done(): - # limits=None should restore everything: restore_limiter.restore_original_limits() - assert lib1.get_num_threads() == orig_limit1 - assert lib2.get_num_threads() == orig_limit2 request.addfinalizer(done) @@ -1674,6 +1672,16 @@ def done(): # This should only overwrite limits for api2, not api1: limiter2.restore_original_limits() assert lib1.get_num_threads() == orig_limit1 - # The limit for api2 was 1 when limiter2 was created, so that's what it - # should restore to: - assert lib2.get_num_threads() == 1 + assert lib2.get_num_threads() == orig_limit2 + + # limit=None should restore everthing: + limiter_all = threadpool_limits(limits=None) + 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. From e13a83ea59d12b49c395bbee4103203d0c50c83e Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Wed, 18 Nov 2026 11:42:33 -0500 Subject: [PATCH 4/4] Test get_original_num_threads() too --- tests/test_threadpoolctl.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index 2cdbdf2c..cb353b69 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -1662,7 +1662,9 @@ def done(): 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() @@ -1676,6 +1678,10 @@ def done(): # 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)