Skip to content

[BUG]: Caching of load_nvidia_dynamic_lib result implies "never close the handle". #1011

@rwgk

Description

@rwgk

Summary

cuda.pathfinder.load_nvidia_dynamic_lib(libname) returns a LoadedDL whose OS handle (_handle_uint) must not be closed by callers. The function is decorated with functools.cache, so the same handle instance is shared across
the process. If user code calls dlclose (Linux) or FreeLibrary (Windows) on that handle, it can unload the module while other code still uses it, resulting in crashes or subtle failures.

This behavior is by design of the current caching, but it was not explicitly documented until now.

Affected platforms

  • Linux and Windows (these are the only supported platforms for this loader).

How it shows up

  • After calling load_nvidia_dynamic_lib(...), code looks up symbols via dlsym / GetProcAddress, then unloads the library.

  • Later, other code (or a subsequent call to load_nvidia_dynamic_lib) uses the cached handle and crashes or fails to resolve symbols.

Minimal example (illustrative)

from cuda.pathfinder import load_nvidia_dynamic_lib

loaded = load_nvidia_dynamic_lib("cudart")
handle = loaded._handle_uint

# ❌ Do not do this – it can unload a cached, shared handle:
#   Linux: ctypes.CDLL("libdl.so").dlclose(ctypes.c_void_p(handle))
#   Windows: kernel32.FreeLibrary(ctypes.c_void_p(handle))

# Later use of the cached handle may now fail or crash.

Metadata

Metadata

Assignees

Labels

cuda.pathfinderEverything related to the cuda.pathfinder module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions