Skip to content

Commit 0bcce3f

Browse files
authored
fix(library): avoid spurious close of cached shared library when calling cudart.getLocalRuntimeVersion (#1010)
* test: add failing test of free-threading loading of SO * fix(library): avoid spurious close of cached shared library * test: deduplicate * fix(library): alternative implementation that cleans up using a finalizer * revert: fix(library): alternative implementation that cleans up using a finalizer This reverts commit 73c43ae.
1 parent 09bac29 commit 0bcce3f

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

cuda_bindings/cuda/bindings/cyruntime.pyx.in

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,13 +1917,19 @@ cdef cudaError_t getLocalRuntimeVersion(int* runtimeVersion) except ?cudaErrorCa
19171917
cdef cudaError_t err = cudaSuccess
19181918
err = (<cudaError_t (*)(int*) except ?cudaErrorCallRequiresNewerDriver nogil> __cudaRuntimeGetVersion)(runtimeVersion)
19191919

1920-
# Unload
1921-
{{if 'Windows' == platform.system()}}
1922-
windll.FreeLibrary(handle)
1923-
{{else}}
1924-
dlfcn.dlclose(handle)
1925-
{{endif}}
1920+
# We explicitly do *NOT* cleanup the library handle here, acknowledging
1921+
# that, yes, the handle leaks. The reason is that there's a
1922+
# `functools.cache` on the top-level caller of this function.
1923+
#
1924+
# This means this library would be opened once and then immediately closed,
1925+
# all the while remaining in the cache lurking there for people to call.
1926+
#
1927+
# Since we open the library one time (technically once per unique library name),
1928+
# there's not a ton of leakage, which we deem acceptable for the 1000x speedup
1929+
# achieved by caching (ultimately) `ctypes.CDLL` calls.
1930+
#
1931+
# Long(er)-term we can explore cleaning up the library using higher-level
1932+
# Python mechanisms, like `__del__` or `weakref.finalizer`s.
19261933

1927-
# Return
19281934
return err
19291935
{{endif}}

cuda_bindings/tests/test_cudart.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,10 +1404,12 @@ def test_struct_pointer_comparison(target):
14041404

14051405

14061406
def test_getLocalRuntimeVersion():
1407-
try:
1408-
err, version = cudart.getLocalRuntimeVersion()
1409-
except pathfinder.DynamicLibNotFoundError:
1410-
pytest.skip("cudart dynamic lib not available")
1411-
else:
1412-
assertSuccess(err)
1413-
assert version >= 12000 # CUDA 12.0
1407+
# verify that successive calls do not segfault the interpreter
1408+
for _ in range(10):
1409+
try:
1410+
err, version = cudart.getLocalRuntimeVersion()
1411+
except pathfinder.DynamicLibNotFoundError:
1412+
pytest.skip("cudart dynamic lib not available")
1413+
else:
1414+
assertSuccess(err)
1415+
assert version >= 12000 # CUDA 12.0

0 commit comments

Comments
 (0)