Skip to content

Commit 09a594c

Browse files
committed
fix(library): avoid spurious close of cached shared library
1 parent bc41afe commit 09a594c

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
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}}

0 commit comments

Comments
 (0)