diff --git a/cuda_bindings/cuda/bindings/cyruntime.pyx.in b/cuda_bindings/cuda/bindings/cyruntime.pyx.in index df85a806c..3031e43d2 100644 --- a/cuda_bindings/cuda/bindings/cyruntime.pyx.in +++ b/cuda_bindings/cuda/bindings/cyruntime.pyx.in @@ -1917,13 +1917,19 @@ cdef cudaError_t getLocalRuntimeVersion(int* runtimeVersion) except ?cudaErrorCa cdef cudaError_t err = cudaSuccess err = ( __cudaRuntimeGetVersion)(runtimeVersion) - # Unload - {{if 'Windows' == platform.system()}} - windll.FreeLibrary(handle) - {{else}} - dlfcn.dlclose(handle) - {{endif}} + # We explicitly do *NOT* cleanup the library handle here, acknowledging + # that, yes, the handle leaks. The reason is that there's a + # `functools.cache` on the top-level caller of this function. + # + # This means this library would be opened once and then immediately closed, + # all the while remaining in the cache lurking there for people to call. + # + # Since we open the library one time (technically once per unique library name), + # there's not a ton of leakage, which we deem acceptable for the 1000x speedup + # achieved by caching (ultimately) `ctypes.CDLL` calls. + # + # Long(er)-term we can explore cleaning up the library using higher-level + # Python mechanisms, like `__del__` or `weakref.finalizer`s. - # Return return err {{endif}} diff --git a/cuda_bindings/tests/test_cudart.py b/cuda_bindings/tests/test_cudart.py index 6f8fc009e..441645a8d 100644 --- a/cuda_bindings/tests/test_cudart.py +++ b/cuda_bindings/tests/test_cudart.py @@ -1404,10 +1404,12 @@ def test_struct_pointer_comparison(target): def test_getLocalRuntimeVersion(): - try: - err, version = cudart.getLocalRuntimeVersion() - except pathfinder.DynamicLibNotFoundError: - pytest.skip("cudart dynamic lib not available") - else: - assertSuccess(err) - assert version >= 12000 # CUDA 12.0 + # verify that successive calls do not segfault the interpreter + for _ in range(10): + try: + err, version = cudart.getLocalRuntimeVersion() + except pathfinder.DynamicLibNotFoundError: + pytest.skip("cudart dynamic lib not available") + else: + assertSuccess(err) + assert version >= 12000 # CUDA 12.0