diff --git a/CHANGES.md b/CHANGES.md index 276b5945..a5509149 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,13 +1,19 @@ 3.9.0 (under development) ========================= +- 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 easy to set correct limits from Python worker threads and fix a number of race conditions in OpenBLAS-pthreads/win32 controlling thread logic, that could result in unexpected number of threads being set. https://github.com/joblib/threadpoolctl/pull/300 - 3.8.0 (2026-11-05) ================== diff --git a/tests/test_threadpoolctl.py b/tests/test_threadpoolctl.py index 86e7b84f..c8831b93 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,7 @@ 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.info() == original_limits @pytest.mark.parametrize("limit", [[], {}, "sequential_blas_under_openmp", None]) @@ -1821,3 +1822,68 @@ 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.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") + 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() + 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") + + # Best effort to restore the a neutral state in case of test + # failure. + restore_limiter = threadpool_limits(limits=None) + 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: + 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 { + k: v + for k, v in limiter_all.get_original_num_threads().items() + if k in (api1, api2) + } == { + 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 6f45463a..eba58089 100644 --- a/threadpoolctl.py +++ b/threadpoolctl.py @@ -1138,6 +1138,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 @@ -1357,7 +1360,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. @@ -1523,7 +1529,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. @@ -1563,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 ( @@ -1582,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),