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/9] 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/9] 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/9] 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/9] 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) From d1683205371a058014e66b2e69f0e7ade333ce80 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Sat, 21 Nov 2026 14:27:48 -0500 Subject: [PATCH 5/9] Switch versions --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 12cb06f6..75701b6a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,4 +1,4 @@ -3.8.1 (under development) +3.9.0 (under development) ========================= - Fix a bug in `restore_original_limits()` where all limits were always From d2c6c8227e138f25c27fb023b119291fda397936 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Mon, 23 Nov 2026 11:02:31 -0500 Subject: [PATCH 6/9] Match new logic in limit() --- tests/test_threadpoolctl.py | 19 +++++++++++++------ threadpoolctl.py | 10 +++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index a15905b9..a39d612d 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -1090,12 +1090,13 @@ def test_limit_in_worker_thread_integer(user_api): "(direct + FlexiBLAS) which causes test failures." ) - controller = ThreadpoolController() - original_limits = controller.info() + full_controller = ThreadpoolController() + expected_impact_controller = full_controller.select(user_api=user_api) + original_limits = expected_impact_controller.info() - limiter = controller.limit_in_worker_thread(limits=1, user_api=user_api) + limiter = full_controller.limit_in_worker_thread(limits=1, user_api=user_api) assert isinstance(limiter, _ThreadpoolLimiter) - for lib, info in zip(controller.lib_controllers, original_limits): + for lib, info in zip(expected_impact_controller.lib_controllers, original_limits): # If this logic ever grows beyond OpenBLAS, this check should be made # more general: if isinstance(lib, OpenBLASController) and lib.threading_layer != "openmp": @@ -1104,7 +1105,9 @@ def test_limit_in_worker_thread_integer(user_api): else: assert lib.get_num_threads() == 1 limiter.restore_original_limits() - assert controller.info() == original_limits + assert ( + expected_impact_controller.select(user_api=user_api).info() == original_limits + ) @pytest.mark.parametrize("limit", [[], {}, "sequential_blas_under_openmp", None]) @@ -1859,7 +1862,11 @@ def done(): # limit=None should restore everthing: limiter_all = threadpool_limits(limits=None) - assert limiter_all.get_original_num_threads() == { + assert { + k: v + for k, v in limiter_all.get_original_num_threads().items() + if k in (api1, api2) + } == { api1: orig_limit1, api2: orig_limit2, } diff --git a/threadpoolctl.py b/threadpoolctl.py index 14159014..eba58089 100644 --- a/threadpoolctl.py +++ b/threadpoolctl.py @@ -1572,9 +1572,14 @@ def limit_in_worker_thread(self, *, limits=None, user_api=None): "only int is supported as limit for limit_in_worker_thread()" ) + if user_api is None: + controller = self + else: + controller = self.select(user_api=user_api) + new_limits = [] - for lib_controller in self.lib_controllers: + for lib_controller in controller.lib_controllers: # If this logic grows beyond OpenBLAS, maybe add a new API to # LibController to detect shared process pools: if ( @@ -1591,8 +1596,7 @@ def limit_in_worker_thread(self, *, limits=None, user_api=None): "num_threads": limits, } ) - - return self.limit(limits=new_limits, user_api=user_api) + return controller.limit(limits=new_limits, user_api=user_api) @_format_docstring( USER_APIS=", ".join(f'"{api}"' for api in _ALL_USER_APIS), From 10091b5449f05b359755d8441227f5cea628676a Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Mon, 23 Nov 2026 11:37:18 -0500 Subject: [PATCH 7/9] Don't bother on FlexiBLAS, the logic is shared so a bit less testing coverage is OK --- tests/test_threadpoolctl.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index a39d612d..2786e184 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -1828,6 +1828,13 @@ def test_linux_does_not_import_ctypes_util(): @pytest.mark.parametrize(["api1", "api2"], [("blas", "openmp"), ("openmp", "blas")]) def test_restore_only_restores_modified_limits(api1, api2, request): + flexiblas_controller = ThreadpoolController().select(internal_api="flexiblas") + if flexiblas_controller: + pytest.skip( + "FlexiBLAS has issues with get_num_threads() because of " + "different behavior from wrapped OpenBLAS" + ) + try: lib1 = ThreadpoolController().select(user_api=api1).lib_controllers[0] orig_limit1 = lib1.get_num_threads() From e9457f7c6ac262eb0a591401209321ede530a968 Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Tue, 24 Nov 2026 08:52:04 -0500 Subject: [PATCH 8/9] Apply batched suggestions from code review Co-authored-by: Olivier Grisel --- CHANGES.md | 9 +++++---- tests/test_threadpoolctl.py | 13 ++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 56662d9b..a5509149 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,10 +1,11 @@ 3.9.0 (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. +- Fix a bug in `limiter.restore_original_limits()` where all limits + were always restored, even if `limiter` is scoped to a strict subset + of the controllable libraries. Similarly, + `limiter.get_original_num_threads()` will only return original + values for those values that can be controlled by `limiter`. https://github.com/joblib/threadpoolctl/pull/309 - Introduce the `ThreadpoolController.limit_in_worker_thread` method to make it diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index 2786e184..f0e154d5 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -1826,6 +1826,9 @@ def test_linux_does_not_import_ctypes_util(): subprocess.check_call([sys.executable, "-c", script], env=env) +@pytest.mark.skipif( + not cython_extensions_compiled, reason="Requires cython extensions to be compiled" +) @pytest.mark.parametrize(["api1", "api2"], [("blas", "openmp"), ("openmp", "blas")]) def test_restore_only_restores_modified_limits(api1, api2, request): flexiblas_controller = ThreadpoolController().select(internal_api="flexiblas") @@ -1845,16 +1848,16 @@ def test_restore_only_restores_modified_limits(api1, api2, request): if orig_limit1 < 3 or orig_limit2 < 3: pytest.skip("need at least 3 cores") + # Best effort to restore the a neutral state in case of test + # failure. restore_limiter = threadpool_limits(limits=None) - - def done(): - restore_limiter.restore_original_limits() - - request.addfinalizer(done) + request.addfinalizer(restore_limiter.restore_original_limits) limiter1 = threadpool_limits(1, user_api=api1) + assert lib1.get_num_threads() == 1 assert limiter1.get_original_num_threads() == {api1: orig_limit1} limiter2 = threadpool_limits(2, user_api=api2) + assert lib2.get_num_threads() == 2 assert limiter2.get_original_num_threads() == {api2: orig_limit2} # This should only overwrite limits for api1, not api2: From 7f439de0a2b40f624ba56bb2c3059017f6eea2fe Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Tue, 24 Nov 2026 08:53:18 -0500 Subject: [PATCH 9/9] Remove unneeded select() --- tests/test_threadpoolctl.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index f0e154d5..c8831b93 100644 --- a/tests/test_threadpoolctl.py +++ b/tests/test_threadpoolctl.py @@ -1105,9 +1105,7 @@ def test_limit_in_worker_thread_integer(user_api): else: assert lib.get_num_threads() == 1 limiter.restore_original_limits() - assert ( - expected_impact_controller.select(user_api=user_api).info() == original_limits - ) + assert expected_impact_controller.info() == original_limits @pytest.mark.parametrize("limit", [[], {}, "sequential_blas_under_openmp", None])