Skip to content

Commit edff6ec

Browse files
committed
Make interaction between test_load_nvidia_dynamic_lib.py and child_load_nvidia_dynamic_lib_helper.py more robust to make it less likely that bugs like 2642d35 slip in.
1 parent 2642d35 commit edff6ec

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

cuda_pathfinder/tests/child_load_nvidia_dynamic_lib_helper.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
# lightweight module. That avoids re-importing the test module (and
66
# repeating its potentially expensive setup) in every child process.
77

8+
import json
89
import os
910
import sys
11+
import traceback
1012

1113

1214
def build_child_process_failed_for_libname_message(libname, result):
@@ -24,15 +26,20 @@ def validate_abs_path(abs_path):
2426

2527

2628
def child_process_func(libname):
27-
from cuda.pathfinder import load_nvidia_dynamic_lib
29+
from cuda.pathfinder import DynamicLibNotFoundError, load_nvidia_dynamic_lib
2830
from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import _load_lib_no_cache
2931
from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import (
3032
IS_WINDOWS,
3133
SUPPORTED_LINUX_SONAMES,
3234
SUPPORTED_WINDOWS_DLLS,
3335
)
3436

35-
loaded_dl_fresh = load_nvidia_dynamic_lib(libname)
37+
try:
38+
loaded_dl_fresh = load_nvidia_dynamic_lib(libname)
39+
except DynamicLibNotFoundError:
40+
sys.stdout.write("CHILD_LOAD_NVIDIA_DYNAMIC_LIB_HELPER_DYNAMIC_LIB_NOT_FOUND_ERROR:\n")
41+
traceback.print_exc(file=sys.stdout)
42+
return
3643
if loaded_dl_fresh.was_already_loaded_from_elsewhere:
3744
raise RuntimeError("loaded_dl_fresh.was_already_loaded_from_elsewhere")
3845
validate_abs_path(loaded_dl_fresh.abs_path)
@@ -50,4 +57,4 @@ def child_process_func(libname):
5057
raise RuntimeError(f"not os.path.samefile({loaded_dl_no_cache.abs_path=!r}, {loaded_dl_fresh.abs_path=!r})")
5158
validate_abs_path(loaded_dl_no_cache.abs_path)
5259

53-
sys.stdout.write(f"{loaded_dl_fresh.abs_path!r}\n")
60+
sys.stdout.write(json.dumps(loaded_dl_fresh.abs_path) + "\n")

cuda_pathfinder/tests/test_load_nvidia_dynamic_lib.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
import functools
5+
import json
56
import os
67
from unittest.mock import patch
78

@@ -98,9 +99,18 @@ def test_load_nvidia_dynamic_lib(info_summary_append, libname):
9899
# interfere across test cases and lead to nondeterministic or platform-specific failures.
99100
timeout = 120 if supported_nvidia_libs.IS_WINDOWS else 30
100101
result = spawned_process_runner.run_in_spawned_child_process(child_process_func, args=(libname,), timeout=timeout)
101-
if result.returncode == 0:
102-
info_summary_append(f"abs_path={result.stdout.rstrip()}")
103-
elif STRICTNESS == "see_what_works" and "DynamicLibNotFoundError: Failure finding " in result.stderr:
102+
103+
def raise_child_process_failed():
104+
raise RuntimeError(build_child_process_failed_for_libname_message(libname, result))
105+
106+
if result.returncode != 0:
107+
raise_child_process_failed()
108+
assert not result.stderr
109+
if result.stdout.startswith("CHILD_LOAD_NVIDIA_DYNAMIC_LIB_HELPER_DYNAMIC_LIB_NOT_FOUND_ERROR:"):
110+
if STRICTNESS == "all_must_work":
111+
raise_child_process_failed()
104112
info_summary_append(f"Not found: {libname=!r}")
105113
else:
106-
raise RuntimeError(build_child_process_failed_for_libname_message(libname, result))
114+
abs_path = json.loads(result.stdout.rstrip())
115+
info_summary_append(f"{abs_path=}")
116+
assert os.path.isfile(abs_path) # double-check the abs_path

0 commit comments

Comments
 (0)