Skip to content

Commit 0443816

Browse files
authored
Make add_dll_directory(), load_dependencies() side-effects more deterministic. (#855)
1 parent 2f99cfa commit 0443816

File tree

4 files changed

+22
-42
lines changed

4 files changed

+22
-42
lines changed

cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_linux.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,7 @@ def get_candidate_sonames(libname: str) -> list[str]:
127127
return candidate_sonames
128128

129129

130-
def check_if_already_loaded_from_elsewhere(libname: str) -> Optional[LoadedDL]:
131-
"""Check if the library is already loaded in the process.
132-
133-
Args:
134-
libname: The name of the library to check
135-
136-
Returns:
137-
A LoadedDL object if the library is already loaded, None otherwise
138-
139-
Example:
140-
>>> loaded = check_if_already_loaded_from_elsewhere("cudart")
141-
>>> if loaded is not None:
142-
... print(f"Library already loaded from {loaded.abs_path}")
143-
"""
144-
130+
def check_if_already_loaded_from_elsewhere(libname: str, _have_abs_path: bool) -> Optional[LoadedDL]:
145131
for soname in get_candidate_sonames(libname):
146132
try:
147133
handle = ctypes.CDLL(soname, mode=os.RTLD_NOLOAD)

cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_dl_windows.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,16 @@ def abs_path_for_dynamic_library(libname: str, handle: ctypes.wintypes.HMODULE)
100100
return buffer.value
101101

102102

103-
def check_if_already_loaded_from_elsewhere(libname: str) -> Optional[LoadedDL]:
104-
"""Check if the library is already loaded in the process.
105-
106-
Args:
107-
libname: The name of the library to check
108-
109-
Returns:
110-
A LoadedDL object if the library is already loaded, None otherwise
111-
112-
Example:
113-
>>> loaded = check_if_already_loaded_from_elsewhere("cudart")
114-
>>> if loaded is not None:
115-
... print(f"Library already loaded from {loaded.abs_path}")
116-
"""
117-
103+
def check_if_already_loaded_from_elsewhere(libname: str, have_abs_path: bool) -> Optional[LoadedDL]:
118104
for dll_name in SUPPORTED_WINDOWS_DLLS.get(libname, ()):
119105
handle = kernel32.GetModuleHandleW(dll_name)
120106
if handle:
121107
abs_path = abs_path_for_dynamic_library(libname, handle)
108+
if have_abs_path and libname in LIBNAMES_REQUIRING_OS_ADD_DLL_DIRECTORY:
109+
# This is a side-effect if the pathfinder loads the library via
110+
# load_with_abs_path(). To make the side-effect more deterministic,
111+
# activate it even if the library was already loaded from elsewhere.
112+
add_dll_directory(abs_path)
122113
return LoadedDL(abs_path, True, ctypes_handle_to_unsigned_int(handle))
123114
return None
124115

cuda_pathfinder/cuda/pathfinder/_dynamic_libs/load_nvidia_dynamic_lib.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,29 @@
2424

2525

2626
def _load_lib_no_cache(libname: str) -> LoadedDL:
27-
# Check whether the library is already loaded into the current process by
28-
# some other component. This check uses OS-level mechanisms (e.g.,
29-
# dlopen on Linux, GetModuleHandle on Windows).
30-
loaded = check_if_already_loaded_from_elsewhere(libname)
31-
if loaded is not None:
32-
return loaded
27+
found = _FindNvidiaDynamicLib(libname)
28+
have_abs_path = found.abs_path is not None
29+
30+
# If the library was already loaded by someone else, reproduce any OS-specific
31+
# side-effects we would have applied on a direct absolute-path load (e.g.,
32+
# AddDllDirectory on Windows for libs that require it).
33+
loaded = check_if_already_loaded_from_elsewhere(libname, have_abs_path)
3334

34-
# Load dependencies first
35+
# Load dependencies regardless of who loaded the primary lib first.
36+
# Doing this *after* the side-effect ensures dependencies resolve consistently
37+
# relative to the actually loaded location.
3538
load_dependencies(libname, load_nvidia_dynamic_lib)
3639

37-
# Find the library path
38-
found = _FindNvidiaDynamicLib(libname)
39-
if found.abs_path is None:
40+
if loaded is not None:
41+
return loaded
42+
43+
if not have_abs_path:
4044
loaded = load_with_system_search(libname)
4145
if loaded is not None:
4246
return loaded
4347
found.retry_with_cuda_home_priority_last()
4448
found.raise_if_abs_path_is_None()
4549

46-
# Load the library from the found path
4750
assert found.abs_path is not None # for mypy
4851
return load_with_abs_path(libname, found.abs_path)
4952

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4-
__version__ = "1.1.1a1"
4+
__version__ = "1.1.1a2"

0 commit comments

Comments
 (0)