From b88041c02f13914aeee58ca6222715fa89ec8bac Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Wed, 25 Nov 2026 16:18:11 -0700 Subject: [PATCH 1/2] refactor(pathfinder): give the CTK-root canary probe its own module The probe that finds the CUDA Toolkit root lived inside the library loader, but header and binary discovery use it too. Headers imported a private function from the loader to get at it, and binaries imported the loader lazily. The probe now lives in `_dynamic_libs/canary_probe.py`. The code is moved byte for byte. The loader, headers and binaries all import `resolve_ctk_root_via_canary` from there, and the header step calls it instead of repeating its two lines. `load_nvidia_dynamic_lib.py` is left with loading only (330 -> 246 lines since the start of the stack). Tests: patches and imports of the probe point at the new module. The old names are not re-exported, so a patch aimed at the old location fails loudly instead of silently doing nothing. No behavior change. 1293 passed, 6 skipped, and the comparisons against `main` are still byte-identical, including simulated Windows. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- cuda_pathfinder/AGENTS.md | 3 + .../_binaries/find_nvidia_binary_utility.py | 2 +- .../pathfinder/_dynamic_libs/canary_probe.py | 98 +++++++++++++++++++ .../_dynamic_libs/load_nvidia_dynamic_lib.py | 85 +--------------- .../_headers/find_nvidia_headers.py | 10 +- cuda_pathfinder/tests/conftest.py | 6 +- .../tests/test_ctk_root_discovery.py | 47 ++++----- .../tests/test_find_nvidia_headers.py | 13 ++- ...st_load_nvidia_dynamic_lib_using_mocker.py | 11 +-- 9 files changed, 143 insertions(+), 132 deletions(-) create mode 100644 cuda_pathfinder/cuda/pathfinder/_dynamic_libs/canary_probe.py diff --git a/cuda_pathfinder/AGENTS.md b/cuda_pathfinder/AGENTS.md index 6fbaa9ff49c..33f94119e24 100644 --- a/cuda_pathfinder/AGENTS.md +++ b/cuda_pathfinder/AGENTS.md @@ -28,6 +28,9 @@ Windows. - **Load orchestration**: - `load_nvidia_dynamic_lib.py` coordinates find/load phases, dependency loading, driver-lib fast path, and cache semantics. +- **CTK-root canary probe**: + - `canary_probe.py` finds the CUDA Toolkit root by loading a canary library + in a fresh Python process. Library, header and binary discovery share it. - **Static and bitcode libraries**: - `_static_libs/locate_file.py` holds the one search that `find_static_lib.py` and `find_bitcode_lib.py` share. diff --git a/cuda_pathfinder/cuda/pathfinder/_binaries/find_nvidia_binary_utility.py b/cuda_pathfinder/cuda/pathfinder/_binaries/find_nvidia_binary_utility.py index 32e70dabdb2..8a0826e1d96 100644 --- a/cuda_pathfinder/cuda/pathfinder/_binaries/find_nvidia_binary_utility.py +++ b/cuda_pathfinder/cuda/pathfinder/_binaries/find_nvidia_binary_utility.py @@ -71,7 +71,7 @@ def _find_windows_compute_sanitizer(ctk_root: str) -> str | None: def _resolve_ctk_root_via_canary() -> str | None: - from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import resolve_ctk_root_via_canary + from cuda.pathfinder._dynamic_libs.canary_probe import resolve_ctk_root_via_canary ctk_root: str | None = resolve_ctk_root_via_canary(CTK_ROOT_CANARY_ANCHOR_LIBNAMES[0]) return ctk_root diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/canary_probe.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/canary_probe.py new file mode 100644 index 00000000000..5595d4fa5e2 --- /dev/null +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/canary_probe.py @@ -0,0 +1,98 @@ +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""The CTK-root canary probe, shared by library, header and binary discovery. + +A "canary" library is loaded by the OS dynamic loader in a fresh Python +process, and the CUDA Toolkit root is derived from where the loader found it. +""" + +from __future__ import annotations + +import functools +import subprocess + +from cuda.pathfinder._dynamic_libs.search_steps import derive_ctk_root +from cuda.pathfinder._dynamic_libs.subprocess_protocol import ( + DYNAMIC_LIB_SUBPROCESS_CWD, + MODE_CANARY, + STATUS_OK, + DynamicLibSubprocessPayload, + build_dynamic_lib_subprocess_command, + parse_dynamic_lib_subprocess_payload, +) + +_CANARY_PROBE_TIMEOUT_SECONDS = 10.0 + + +def _coerce_subprocess_output(output: str | bytes | None) -> str: + if isinstance(output, bytes): + return output.decode(errors="replace") + return "" if output is None else output + + +def _raise_canary_probe_child_process_error( + *, + returncode: int | None = None, + timeout: float | None = None, + stderr: str | bytes | None = None, +) -> None: + if timeout is None: + error_line = f"Canary probe child process exited with code {returncode}." + else: + error_line = f"Canary probe child process timed out after {timeout} seconds." + raise ChildProcessError( + f"{error_line}\n" + "--- stderr-from-child-process ---\n" + f"{_coerce_subprocess_output(stderr)}" + "\n" + ) + + +@functools.cache +def _resolve_system_loaded_abs_path_in_subprocess( + libname: str, + *, + timeout: float = _CANARY_PROBE_TIMEOUT_SECONDS, +) -> str | None: + """Resolve a canary library's absolute path in a fresh Python subprocess.""" + try: + result = subprocess.run( # noqa: S603 - trusted argv: current interpreter + internal probe module + build_dynamic_lib_subprocess_command(MODE_CANARY, libname), + capture_output=True, + text=True, + timeout=timeout, + check=False, + cwd=DYNAMIC_LIB_SUBPROCESS_CWD, + ) + except subprocess.TimeoutExpired as exc: + _raise_canary_probe_child_process_error(timeout=exc.timeout, stderr=exc.stderr) + + if result.returncode != 0: + _raise_canary_probe_child_process_error(returncode=result.returncode, stderr=result.stderr) + + payload: DynamicLibSubprocessPayload = parse_dynamic_lib_subprocess_payload( + result.stdout, + libname=libname, + error_label="Canary probe child process", + ) + abs_path: str | None = payload.abs_path + if payload.status == STATUS_OK: + return abs_path + return None + + +def resolve_ctk_root_via_canary(canary_libname: str) -> str | None: + """Resolve the CUDA Toolkit root from a system-loadable canary library. + + The canary library's absolute path is resolved by the OS dynamic loader in + an isolated subprocess, which honors ``LD_LIBRARY_PATH`` on Linux and the + native DLL search on Windows. The toolkit root is then derived from that + path. Returns ``None`` if the canary cannot be resolved or no root can be + derived. + """ + canary_abs_path = _resolve_system_loaded_abs_path_in_subprocess(canary_libname) + if canary_abs_path is None: + return None + ctk_root: str | None = derive_ctk_root(canary_abs_path) + return ctk_root 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 05804a5d1f8..84311f86240 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 @@ -5,10 +5,10 @@ import functools import struct -import subprocess import sys from typing import TYPE_CHECKING +from cuda.pathfinder._dynamic_libs.canary_probe import resolve_ctk_root_via_canary from cuda.pathfinder._dynamic_libs.lib_descriptor import LIB_DESCRIPTORS from cuda.pathfinder._dynamic_libs.load_dl_common import ( DynamicLibNotAvailableError, @@ -25,18 +25,9 @@ FindResult, NotFound, SearchContext, - derive_ctk_root, find_via_ctk_root, run_find_steps, ) -from cuda.pathfinder._dynamic_libs.subprocess_protocol import ( - DYNAMIC_LIB_SUBPROCESS_CWD, - MODE_CANARY, - STATUS_OK, - DynamicLibSubprocessPayload, - build_dynamic_lib_subprocess_command, - parse_dynamic_lib_subprocess_payload, -) from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import ALL_AVAILABLE_LIBNAMES from cuda.pathfinder._utils.platform_aware import IS_WINDOWS @@ -47,7 +38,6 @@ # (CTK, third-party, driver). _ALL_KNOWN_LIBNAMES: frozenset[str] = frozenset(LIB_DESCRIPTORS) _PLATFORM_NAME = "Windows" if IS_WINDOWS else "Linux" -_CANARY_PROBE_TIMEOUT_SECONDS = 10.0 def _load_driver_lib_no_cache(desc: LibDescriptor) -> LoadedDL: @@ -70,79 +60,6 @@ def _load_driver_lib_no_cache(desc: LibDescriptor) -> LoadedDL: ) -def _coerce_subprocess_output(output: str | bytes | None) -> str: - if isinstance(output, bytes): - return output.decode(errors="replace") - return "" if output is None else output - - -def _raise_canary_probe_child_process_error( - *, - returncode: int | None = None, - timeout: float | None = None, - stderr: str | bytes | None = None, -) -> None: - if timeout is None: - error_line = f"Canary probe child process exited with code {returncode}." - else: - error_line = f"Canary probe child process timed out after {timeout} seconds." - raise ChildProcessError( - f"{error_line}\n" - "--- stderr-from-child-process ---\n" - f"{_coerce_subprocess_output(stderr)}" - "\n" - ) - - -@functools.cache -def _resolve_system_loaded_abs_path_in_subprocess( - libname: str, - *, - timeout: float = _CANARY_PROBE_TIMEOUT_SECONDS, -) -> str | None: - """Resolve a canary library's absolute path in a fresh Python subprocess.""" - try: - result = subprocess.run( # noqa: S603 - trusted argv: current interpreter + internal probe module - build_dynamic_lib_subprocess_command(MODE_CANARY, libname), - capture_output=True, - text=True, - timeout=timeout, - check=False, - cwd=DYNAMIC_LIB_SUBPROCESS_CWD, - ) - except subprocess.TimeoutExpired as exc: - _raise_canary_probe_child_process_error(timeout=exc.timeout, stderr=exc.stderr) - - if result.returncode != 0: - _raise_canary_probe_child_process_error(returncode=result.returncode, stderr=result.stderr) - - payload: DynamicLibSubprocessPayload = parse_dynamic_lib_subprocess_payload( - result.stdout, - libname=libname, - error_label="Canary probe child process", - ) - abs_path: str | None = payload.abs_path - if payload.status == STATUS_OK: - return abs_path - return None - - -def resolve_ctk_root_via_canary(canary_libname: str) -> str | None: - """Resolve the CUDA Toolkit root from a system-loadable canary library. - - The canary library's absolute path is resolved by the OS dynamic loader in - an isolated subprocess, which honors ``LD_LIBRARY_PATH`` on Linux and the - native DLL search on Windows. The toolkit root is then derived from that - path. Returns ``None`` if the canary cannot be resolved or no root can be - derived. - """ - canary_abs_path = _resolve_system_loaded_abs_path_in_subprocess(canary_libname) - if canary_abs_path is None: - return None - ctk_root: str | None = derive_ctk_root(canary_abs_path) - return ctk_root - - def _try_ctk_root_canary(ctx: SearchContext) -> FindResult | NotFound: """Try CTK-root canary fallback for descriptor-configured libraries.""" missed = NotFound() diff --git a/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py b/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py index e3d4ee99d8e..919f52d883c 100644 --- a/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py +++ b/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py @@ -10,10 +10,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( - _resolve_system_loaded_abs_path_in_subprocess, -) -from cuda.pathfinder._dynamic_libs.search_steps import derive_ctk_root +from cuda.pathfinder._dynamic_libs.canary_probe import resolve_ctk_root_via_canary from cuda.pathfinder._headers.header_descriptor import ( HEADER_DESCRIPTORS, platform_include_subdirs, @@ -145,10 +142,7 @@ def find_via_ctk_root_canary(desc: HeaderDescriptor) -> LocatedHeaderDir | None: """ if not desc.use_ctk_root_canary: return None - canary_abs_path = _resolve_system_loaded_abs_path_in_subprocess(CTK_ROOT_CANARY_ANCHOR_LIBNAMES[0]) - if canary_abs_path is None: - return None - ctk_root = derive_ctk_root(canary_abs_path) + ctk_root = resolve_ctk_root_via_canary(CTK_ROOT_CANARY_ANCHOR_LIBNAMES[0]) if ctk_root is None: return None result = _locate_in_anchor_layout(desc, ctk_root) diff --git a/cuda_pathfinder/tests/conftest.py b/cuda_pathfinder/tests/conftest.py index a67ac32184d..543755e31ac 100644 --- a/cuda_pathfinder/tests/conftest.py +++ b/cuda_pathfinder/tests/conftest.py @@ -36,11 +36,9 @@ def skip_if_missing_libnvcudla_so(libname: str, *, timeout: float) -> None: return # Keep the import inside the helper so unrelated import issues do not fail # pytest collection for the whole test suite. - from cuda.pathfinder._dynamic_libs import load_nvidia_dynamic_lib as load_nvidia_dynamic_lib_module + from cuda.pathfinder._dynamic_libs.canary_probe import _resolve_system_loaded_abs_path_in_subprocess - canary_abs_path = load_nvidia_dynamic_lib_module._resolve_system_loaded_abs_path_in_subprocess( - "nvcudla", timeout=timeout - ) + canary_abs_path = _resolve_system_loaded_abs_path_in_subprocess("nvcudla", timeout=timeout) if canary_abs_path is not None: return pytest.skip("libnvcudla.so is not loadable via canary subprocess on this host.") diff --git a/cuda_pathfinder/tests/test_ctk_root_discovery.py b/cuda_pathfinder/tests/test_ctk_root_discovery.py index 5f1bdd08411..a0ded90bfd1 100644 --- a/cuda_pathfinder/tests/test_ctk_root_discovery.py +++ b/cuda_pathfinder/tests/test_ctk_root_discovery.py @@ -12,13 +12,15 @@ from cuda.pathfinder._dynamic_libs import load_nvidia_dynamic_lib as load_mod from cuda.pathfinder._dynamic_libs import search_steps as steps_mod +from cuda.pathfinder._dynamic_libs.canary_probe import ( + _resolve_system_loaded_abs_path_in_subprocess, + resolve_ctk_root_via_canary, +) from cuda.pathfinder._dynamic_libs.lib_descriptor import LIB_DESCRIPTORS from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( _load_lib_no_cache, - _resolve_system_loaded_abs_path_in_subprocess, _try_ctk_root_canary, - resolve_ctk_root_via_canary, ) from cuda.pathfinder._dynamic_libs.search_platform import ( linux_search_context, @@ -43,6 +45,7 @@ from cuda.pathfinder._utils.windows_arch import windows_python_arch _MODULE = "cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib" +_PROBE_MODULE = "cuda.pathfinder._dynamic_libs.canary_probe" _STEPS_MODULE = "cuda.pathfinder._dynamic_libs.search_steps" _PACKAGE_ROOT = DYNAMIC_LIB_SUBPROCESS_CWD @@ -246,7 +249,7 @@ def test_subprocess_probe_returns_abs_path_on_string_payload(mocker): stdout='{"status": "ok", "abs_path": "/usr/local/cuda/lib64/libcudart.so.13"}\n', stderr="", ) - run_mock = mocker.patch(f"{_MODULE}.subprocess.run", return_value=result) + run_mock = mocker.patch(f"{_PROBE_MODULE}.subprocess.run", return_value=result) assert _resolve_system_loaded_abs_path_in_subprocess("cudart") == "/usr/local/cuda/lib64/libcudart.so.13" run_mock.assert_called_once_with( @@ -266,14 +269,14 @@ def test_subprocess_probe_returns_none_on_null_payload(mocker): stdout='{"status": "not-found", "abs_path": null}\n', stderr="", ) - mocker.patch(f"{_MODULE}.subprocess.run", return_value=result) + mocker.patch(f"{_PROBE_MODULE}.subprocess.run", return_value=result) assert _resolve_system_loaded_abs_path_in_subprocess("cudart") is None def test_subprocess_probe_raises_on_child_failure(mocker): result = subprocess.CompletedProcess(args=[], returncode=1, stdout="", stderr="child failed\n") - mocker.patch(f"{_MODULE}.subprocess.run", return_value=result) + mocker.patch(f"{_PROBE_MODULE}.subprocess.run", return_value=result) with pytest.raises(ChildProcessError, match="child failed"): _resolve_system_loaded_abs_path_in_subprocess("cudart") @@ -281,7 +284,7 @@ def test_subprocess_probe_raises_on_child_failure(mocker): def test_subprocess_probe_raises_on_timeout(mocker): mocker.patch( - f"{_MODULE}.subprocess.run", + f"{_PROBE_MODULE}.subprocess.run", side_effect=subprocess.TimeoutExpired(cmd=["python"], timeout=10.0, stderr="probe hung\n"), ) with pytest.raises(ChildProcessError, match="timed out after 10.0 seconds"): @@ -290,7 +293,7 @@ def test_subprocess_probe_raises_on_timeout(mocker): def test_subprocess_probe_raises_on_empty_stdout(mocker): result = subprocess.CompletedProcess(args=[], returncode=0, stdout=" \n \n", stderr="") - mocker.patch(f"{_MODULE}.subprocess.run", return_value=result) + mocker.patch(f"{_PROBE_MODULE}.subprocess.run", return_value=result) with pytest.raises(RuntimeError, match="produced no stdout payload"): _resolve_system_loaded_abs_path_in_subprocess("cudart") @@ -298,7 +301,7 @@ def test_subprocess_probe_raises_on_empty_stdout(mocker): def test_subprocess_probe_raises_on_invalid_json_payload(mocker): result = subprocess.CompletedProcess(args=[], returncode=0, stdout="not-json\n", stderr="") - mocker.patch(f"{_MODULE}.subprocess.run", return_value=result) + mocker.patch(f"{_PROBE_MODULE}.subprocess.run", return_value=result) with pytest.raises(RuntimeError, match="invalid JSON payload"): _resolve_system_loaded_abs_path_in_subprocess("cudart") @@ -311,7 +314,7 @@ def test_subprocess_probe_raises_on_unexpected_json_payload(mocker): stdout='{"path": "/usr/local/cuda/lib64/libcudart.so.13"}\n', stderr="", ) - mocker.patch(f"{_MODULE}.subprocess.run", return_value=result) + mocker.patch(f"{_PROBE_MODULE}.subprocess.run", return_value=result) with pytest.raises(RuntimeError, match="unexpected payload"): _resolve_system_loaded_abs_path_in_subprocess("cudart") @@ -325,7 +328,7 @@ def test_subprocess_probe_does_not_reenter_calling_script(tmp_path): f""" from pathlib import Path - from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( + from cuda.pathfinder._dynamic_libs.canary_probe import ( _resolve_system_loaded_abs_path_in_subprocess, ) @@ -374,7 +377,7 @@ def test_canary_finds_nvvm(tmp_path, mocker): nvvm_lib = _create_nvvm_in_ctk(ctk_root) probe = mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=_fake_canary_path(ctk_root), ) parent_system_loader = mocker.patch.object(load_mod.LOADER, "load_with_system_search") @@ -385,13 +388,13 @@ def test_canary_finds_nvvm(tmp_path, mocker): def test_canary_misses_when_subprocess_probe_fails(mocker): - mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) + mocker.patch(f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) assert not _try_ctk_root_canary(_ctx("nvvm")).found def test_canary_misses_when_ctk_root_unrecognized(mocker): mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value="/weird/path/libcudart.so.13", ) assert not _try_ctk_root_canary(_ctx("nvvm")).found @@ -403,14 +406,14 @@ def test_canary_misses_when_nvvm_not_in_ctk_root(tmp_path, mocker): _create_cudart_in_ctk(ctk_root) mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=_fake_canary_path(ctk_root), ) assert not _try_ctk_root_canary(_ctx("nvvm")).found def test_canary_skips_when_abs_path_none(mocker): - mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) + mocker.patch(f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) assert not _try_ctk_root_canary(_ctx("nvvm")).found @@ -423,7 +426,7 @@ def test_resolve_ctk_root_via_canary_returns_root(tmp_path, mocker): ctk_root = tmp_path / "cuda-13" _create_cudart_in_ctk(ctk_root) probe = mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=_fake_canary_path(ctk_root), ) assert resolve_ctk_root_via_canary("cudart") == str(ctk_root) @@ -431,13 +434,13 @@ def test_resolve_ctk_root_via_canary_returns_root(tmp_path, mocker): def test_resolve_ctk_root_via_canary_none_when_probe_fails(mocker): - mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) + mocker.patch(f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) assert resolve_ctk_root_via_canary("cudart") is None def test_resolve_ctk_root_via_canary_none_when_unrecognized(mocker): mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=str(Path(os.sep, "weird", "path", "libcudart.so.13")), ) assert resolve_ctk_root_via_canary("cudart") is None @@ -485,7 +488,7 @@ def test_cuda_home_takes_priority_over_canary(tmp_path, mocker): # System search finds nothing for nvvm. mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) # Canary subprocess probe would find cudart if consulted. - mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", side_effect=canary_mock) + mocker.patch(f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", side_effect=canary_mock) # CUDA_HOME points to a separate root that also has nvvm mocker.patch(f"{_STEPS_MODULE}.get_cuda_path_or_home", return_value=str(cuda_home_root)) # Capture the final load call @@ -513,7 +516,7 @@ def test_canary_fires_only_after_all_earlier_steps_fail(tmp_path, mocker): mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) # Canary subprocess probe finds cudart under a system CTK root. mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=_fake_canary_path(canary_root), ) # No CUDA_HOME set @@ -536,7 +539,7 @@ def test_non_discoverable_lib_skips_canary_probe(mocker): # Force fallback path for a lib that is not canary-discoverable. mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) mocker.patch(f"{_STEPS_MODULE}.get_cuda_path_or_home", return_value=None) - canary_probe = mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess") + canary_probe = mocker.patch(f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess") with pytest.raises(DynamicLibNotFoundError): _load_lib_no_cache("cublas") @@ -578,7 +581,7 @@ def find_wheel_dirs(sub_dirs): mocker.patch(f"{_MODULE}.load_dependencies") mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=str(canary_root / "lib64" / "libcudart.so.13"), ) diff --git a/cuda_pathfinder/tests/test_find_nvidia_headers.py b/cuda_pathfinder/tests/test_find_nvidia_headers.py index f74e1bc09c3..ab0b7f7f80c 100644 --- a/cuda_pathfinder/tests/test_find_nvidia_headers.py +++ b/cuda_pathfinder/tests/test_find_nvidia_headers.py @@ -23,12 +23,11 @@ from packaging.requirements import Requirement from packaging.version import Version +import cuda.pathfinder._dynamic_libs.canary_probe as canary_probe_module import cuda.pathfinder._headers.find_nvidia_headers as find_nvidia_headers_module import cuda.pathfinder._headers.header_descriptor as header_descriptor_module from cuda.pathfinder import LocatedHeaderDir, find_nvidia_header_directory, locate_nvidia_header_directory -from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( - _resolve_system_loaded_abs_path_in_subprocess, -) +from cuda.pathfinder._dynamic_libs.canary_probe import _resolve_system_loaded_abs_path_in_subprocess from cuda.pathfinder._headers.header_descriptor import HEADER_DESCRIPTORS, system_install_dir_patterns from cuda.pathfinder._headers.supported_nvidia_headers import SUPPORTED_HEADERS_CTK from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home @@ -395,7 +394,7 @@ def test_locate_ctk_headers_uses_canary_fallback_when_cuda_home_unset(tmp_path, monkeypatch.delenv("CUDA_PATH", raising=False) mocker.patch.object(find_nvidia_headers_module, "find_sub_dirs_all_sitepackages", return_value=[]) probe = mocker.patch.object( - find_nvidia_headers_module, + canary_probe_module, "_resolve_system_loaded_abs_path_in_subprocess", return_value=_fake_cudart_canary_abs_path(ctk_root), ) @@ -420,7 +419,7 @@ def test_locate_ctk_headers_cuda_home_takes_priority_over_canary(tmp_path, monke monkeypatch.delenv("CUDA_PATH", raising=False) mocker.patch.object(find_nvidia_headers_module, "find_sub_dirs_all_sitepackages", return_value=[]) probe = mocker.patch.object( - find_nvidia_headers_module, + canary_probe_module, "_resolve_system_loaded_abs_path_in_subprocess", return_value=_fake_cudart_canary_abs_path(canary_root), ) @@ -440,7 +439,7 @@ def test_locate_ctk_headers_canary_miss_paths_are_non_fatal(monkeypatch, mocker) monkeypatch.delenv("CUDA_PATH", raising=False) mocker.patch.object(find_nvidia_headers_module, "find_sub_dirs_all_sitepackages", return_value=[]) mocker.patch.object( - find_nvidia_headers_module, + canary_probe_module, "_resolve_system_loaded_abs_path_in_subprocess", return_value=None, ) @@ -456,7 +455,7 @@ def test_locate_ctk_headers_canary_probe_errors_are_not_masked(monkeypatch, mock monkeypatch.delenv("CUDA_PATH", raising=False) mocker.patch.object(find_nvidia_headers_module, "find_sub_dirs_all_sitepackages", return_value=[]) mocker.patch.object( - find_nvidia_headers_module, + canary_probe_module, "_resolve_system_loaded_abs_path_in_subprocess", side_effect=RuntimeError("canary probe failed"), ) 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 9680b182d1c..4fbba9bc18e 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 @@ -7,16 +7,15 @@ from cuda.pathfinder._dynamic_libs import load_nvidia_dynamic_lib as load_mod from cuda.pathfinder._dynamic_libs import search_steps as steps_mod +from cuda.pathfinder._dynamic_libs.canary_probe import _resolve_system_loaded_abs_path_in_subprocess from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL -from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import ( - _load_lib_no_cache, - _resolve_system_loaded_abs_path_in_subprocess, -) +from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import _load_lib_no_cache from cuda.pathfinder._dynamic_libs.search_platform import search_context, windows_search_context from cuda.pathfinder._dynamic_libs.search_steps import EARLY_FIND_STEPS, NotFound from cuda.pathfinder._utils.platform_aware import IS_WINDOWS _MODULE = "cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib" +_PROBE_MODULE = "cuda.pathfinder._dynamic_libs.canary_probe" _STEPS_MODULE = "cuda.pathfinder._dynamic_libs.search_steps" @@ -126,7 +125,7 @@ def _run_find_steps_without_site_packages(ctx, steps): mocker.patch(f"{_MODULE}.load_dependencies") mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) mocker.patch(f"{_STEPS_MODULE}.get_cuda_path_or_home", return_value=None) - mocker.patch(f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) + mocker.patch(f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None) mocker.patch.object( load_mod.LOADER, "load_with_abs_path", @@ -158,7 +157,7 @@ def _run_find_steps_disabled(ctx, steps): mocker.patch.object(load_mod.LOADER, "load_with_system_search", return_value=None) mocker.patch(f"{_STEPS_MODULE}.get_cuda_path_or_home", return_value=None) mocker.patch( - f"{_MODULE}._resolve_system_loaded_abs_path_in_subprocess", + f"{_PROBE_MODULE}._resolve_system_loaded_abs_path_in_subprocess", return_value=None, ) From 6d23349066dbe2b1815c2bcfa2d87b812f3d48ed Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 26 Nov 2026 11:17:27 -0800 Subject: [PATCH 2/2] ci: require manual copy-pr-bot approval Signed-off-by: Ralf W. Grosse-Kunstleve