You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Windows: every failed import llama_cpp prepends to PATH and adds DLL directories again #2424
If llama.dll then fails to load, the import raises and Python does not cache the module. The next import llama_cpp runs all of this again. Each failed attempt adds one more PATH entry and four more DLL directories, and none of them are removed.
That matters for long-running apps that check whether llama.cpp is available, for example on a status poll. In our case the app retried the import from a status check. After about three hours the process PATH held the same llama_cpp\lib entry 104 times (12,323 characters). Then an unrelated library failed at its own os.add_dll_directory call with [WinError 206] The filename or extension is too long.
Reproduce
Use any install where llama.dll cannot load. A CUDA 12 wheel on a machine with only CUDA 13 installed does it:
importosfor_inrange(3):
try:
importllama_cppexceptRuntimeErroraserror:
print(type(error).__name__)
print(os.environ["PATH"].count("llama_cpp")) # prints 3, expected 1
Expected
A failed load leaves PATH and the DLL search list as they were, or adds each entry at most once per process.
Possible fix
Keep the handles that os.add_dll_directory returns, and close them when the CDLL call fails.
Prepend to PATH only if the entry is not already there.
base_path is added twice on Python 3.8 and later. One call is enough.
Environment
llama-cpp-python 0.3.37 (CUDA wheel, built against cublas64_12 and cudart64_12)
Activity