From 9550142fc0d68b0d32a586e07e3677087c4cd9ac Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Wed, 25 Nov 2026 16:32:01 -0700 Subject: [PATCH 1/5] fix(pathfinder): check for an already-loaded library before searching the disk Probe the target library before searching site-packages and conda. A hit skips that search and still resolves dependencies. A miss keeps the existing search and dependency order. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin Signed-off-by: Ralf W. Grosse-Kunstleve --- .../_dynamic_libs/load_nvidia_dynamic_lib.py | 7 +++---- .../cuda/pathfinder/_dynamic_libs/search_steps.py | 2 +- .../test_load_nvidia_dynamic_lib_using_mocker.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py index 5a9549472462..94598c5d8340 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py @@ -170,11 +170,10 @@ def _load_lib_no_cache(libname: str) -> LoadedDL: ctx = SearchContext(desc) - # Phase 1: Try to find the library file on disk (pip wheels, conda). - find = run_find_steps(ctx, EARLY_FIND_STEPS) - - # Phase 2: Cross-cutting — already-loaded check and dependency loading. + # An already-loaded library bypasses the wheel and conda search. loaded = LOADER.check_if_already_loaded_from_elsewhere(desc) + find = run_find_steps(ctx, EARLY_FIND_STEPS) if loaded is None else None + load_dependencies(desc, load_nvidia_dynamic_lib) if loaded is not None: return loaded diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py index b9731a74ba00..1511a9f98723 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py @@ -259,7 +259,7 @@ def find_in_program_files_roots(ctx: SearchContext) -> FindResult | None: # Step sequences per strategy # --------------------------------------------------------------------------- -#: Find steps that run before the already-loaded check and system search. +#: Find steps that run after the already-loaded check and before system search. EARLY_FIND_STEPS: tuple[FindStep, ...] = (find_in_site_packages, find_in_conda) #: Find steps that run after system search fails. diff --git a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py index 70a095406785..6c88bc7dda6f 100644 --- a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py +++ b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py @@ -274,3 +274,18 @@ def _run_find_steps_without_site_packages(ctx, steps): result = _load_lib_no_cache("cupti") assert result.found_via == "conda" assert result.abs_path == str(conda_cupti_lib) + + +@pytest.mark.agent_authored(model="claude-fable-5-2") +def test_already_loaded_library_is_returned_without_searching_the_disk(mocker): + """An unreadable search location must not break a library that is already loaded.""" + already_loaded = _make_loaded_dl("/opt/elsewhere/libcublas.so.13", "was-already-loaded-from-elsewhere") + mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=already_loaded) + mocker.patch(f"{_MODULE}.load_dependencies") + unreadable = mocker.patch( + "cuda.pathfinder._dynamic_libs.search_platform.find_sub_dirs_all_sitepackages", + side_effect=PermissionError("site-packages is not readable"), + ) + + assert _load_lib_no_cache("cublas") is already_loaded + unreadable.assert_not_called() From 8bd18411c1801de0a084f8f234c24c6e534a2fb5 Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Thu, 26 Nov 2026 10:37:06 -0700 Subject: [PATCH 2/5] test(pathfinder): preserve dependency handling around the loaded check An already-loaded target still resolves dependencies. For an unloaded target, a discovery failure reaches the caller before dependency loading. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin Signed-off-by: Ralf W. Grosse-Kunstleve --- ...st_load_nvidia_dynamic_lib_using_mocker.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py index 6c88bc7dda6f..91c15533bfb2 100644 --- a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py +++ b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py @@ -289,3 +289,31 @@ def test_already_loaded_library_is_returned_without_searching_the_disk(mocker): assert _load_lib_no_cache("cublas") is already_loaded unreadable.assert_not_called() + + +@pytest.mark.agent_authored(model="claude-fable-5-2") +def test_already_loaded_library_still_loads_its_dependencies(mocker): + """Skipping the search must not skip the dependencies.""" + already_loaded = _make_loaded_dl("/opt/elsewhere/libcublas.so.13", "was-already-loaded-from-elsewhere") + mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=already_loaded) + load_dependencies = mocker.patch(f"{_MODULE}.load_dependencies") + run_find_steps = mocker.patch(f"{_MODULE}.run_find_steps") + + assert _load_lib_no_cache("cublas") is already_loaded + load_dependencies.assert_called_once() + run_find_steps.assert_not_called() + + +@pytest.mark.agent_authored(model="gpt-6") +def test_unloaded_library_search_failure_precedes_dependency_loading(mocker): + probe = mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None) + failure = PermissionError("site-packages is not readable") + mocker.patch(f"{_MODULE}.run_find_steps", side_effect=failure) + load_dependencies = mocker.patch(f"{_MODULE}.load_dependencies") + + with pytest.raises(PermissionError) as exc_info: + _load_lib_no_cache("cublas") + + assert exc_info.value is failure + probe.assert_called_once() + load_dependencies.assert_not_called() From 438058dbe807091aa2c9ce4ae11221c091fcd6c2 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 26 Nov 2026 11:17:25 -0800 Subject: [PATCH 3/5] ci: require manual copy-pr-bot approval Signed-off-by: Ralf W. Grosse-Kunstleve From 63c36ab45f66c9c34f60ec252a48a2ee27b5d9bb Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 26 Nov 2026 11:58:10 -0800 Subject: [PATCH 4/5] fix(pathfinder): detect libraries loaded during discovery Probe again after the wheel and conda search on an initial miss. Reuse a library loaded by another component during discovery and preserve the already-loaded fast path. Signed-off-by: Ralf W. Grosse-Kunstleve --- .../_dynamic_libs/load_nvidia_dynamic_lib.py | 6 +++- ...st_load_nvidia_dynamic_lib_using_mocker.py | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py index 94598c5d8340..56c94fec415b 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py @@ -172,7 +172,11 @@ def _load_lib_no_cache(libname: str) -> LoadedDL: # An already-loaded library bypasses the wheel and conda search. loaded = LOADER.check_if_already_loaded_from_elsewhere(desc) - find = run_find_steps(ctx, EARLY_FIND_STEPS) if loaded is None else None + find = None + if loaded is None: + find = run_find_steps(ctx, EARLY_FIND_STEPS) + # Another component may have loaded the library during discovery. + loaded = LOADER.check_if_already_loaded_from_elsewhere(desc) load_dependencies(desc, load_nvidia_dynamic_lib) if loaded is not None: diff --git a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py index 91c15533bfb2..7f37518a27d7 100644 --- a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py +++ b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py @@ -304,6 +304,39 @@ def test_already_loaded_library_still_loads_its_dependencies(mocker): run_find_steps.assert_not_called() +@pytest.mark.parametrize("find_succeeds", [True, False], ids=["wheel-found", "wheel-missed"]) +@pytest.mark.agent_authored(model="gpt-6") +def test_library_loaded_during_early_search_reuses_existing_handle(tmp_path, mocker, find_succeeds): + desc = load_mod.LIB_DESCRIPTORS["cublas"] + filename = desc.windows_dlls[0] if IS_WINDOWS else desc.linux_sonames[0] + already_loaded = LoadedDL(str(tmp_path / "external" / filename), True, 0xBEEF, "was-already-loaded-from-elsewhere") + probe = mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None) + load_dependencies = mocker.patch(f"{_MODULE}.load_dependencies") + calls = mocker.Mock() + calls.attach_mock(probe, "probe") + calls.attach_mock(load_dependencies, "load_dependencies") + + def discover(_ctx, steps): + assert steps is EARLY_FIND_STEPS + probe.return_value = already_loaded + if find_succeeds: + return steps_mod.FindResult(str(tmp_path / "wheel" / filename), "site-packages") + return None + + mocker.patch(f"{_MODULE}.run_find_steps", side_effect=discover) + load_wheel = mocker.patch.object(load_mod.LOADER, "load_with_abs_path") + load_native = mocker.patch.object(load_mod.LOADER, "load_with_system_search") + + assert _load_lib_no_cache("cublas") is already_loaded + load_wheel.assert_not_called() + load_native.assert_not_called() + assert calls.mock_calls == [ + mocker.call.probe(desc), + mocker.call.probe(desc), + mocker.call.load_dependencies(desc, load_mod.load_nvidia_dynamic_lib), + ] + + @pytest.mark.agent_authored(model="gpt-6") def test_unloaded_library_search_failure_precedes_dependency_loading(mocker): probe = mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None) From 2afbe62ae6edc553845c249610b8735c4cbdb1c7 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 26 Nov 2026 11:59:13 -0800 Subject: [PATCH 5/5] test(pathfinder): cover dependency order on library discovery Require the cuBLAS dependency to be resolved before loading the target discovered in a wheel or conda environment. Signed-off-by: Ralf W. Grosse-Kunstleve --- ...test_load_nvidia_dynamic_lib_using_mocker.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py index 7f37518a27d7..3dae4cb48bcb 100644 --- a/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py +++ b/cuda_pathfinder/tests/test_load_nvidia_dynamic_lib_using_mocker.py @@ -174,7 +174,7 @@ def _run_find_steps_disabled(ctx, steps): @pytest.mark.parametrize("preferred_source", ("site-packages", "conda", "system-search")) @pytest.mark.agent_authored(model="gpt-6.1-sol") def test_cuda_path_is_a_fallback_to_environment_and_native_search(tmp_path, mocker, monkeypatch, preferred_source): - desc = load_mod.LIB_DESCRIPTORS["cublasLt"] + desc = load_mod.LIB_DESCRIPTORS["cublas"] platform = steps_mod.PLATFORM filename = desc.windows_dlls[0] if IS_WINDOWS else desc.linux_sonames[0] @@ -208,20 +208,21 @@ def find_wheel_dirs(sub_dirs): side_effect=find_wheel_dirs, ) mocker.patch.object(load_mod.LOADER, "check_if_already_loaded_from_elsewhere", return_value=None) - mocker.patch(f"{_MODULE}.load_dependencies") + load_dependency = mocker.patch(f"{_MODULE}.load_nvidia_dynamic_lib") native_path = tmp_path / "native" / filename native_search = mocker.patch.object( load_mod.LOADER, "load_with_system_search", return_value=_make_loaded_dl(str(native_path), "system-search"), ) - mocker.patch.object( - load_mod.LOADER, - "load_with_abs_path", - side_effect=lambda _desc, path, via: _make_loaded_dl(path, via), - ) - loaded = _load_lib_no_cache("cublasLt") + def load_from_discovered_path(_desc, path, via): + load_dependency.assert_called_once_with("cublasLt") + return _make_loaded_dl(path, via) + + mocker.patch.object(load_mod.LOADER, "load_with_abs_path", side_effect=load_from_discovered_path) + + loaded = _load_lib_no_cache("cublas") expected_path = {"site-packages": wheel_path, "conda": conda_path, "system-search": native_path}[preferred_source] assert loaded.found_via == preferred_source