Windows: every failed `import llama_cpp` prepends to PATH and adds DLL directories again
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
Hướng nghiên cứu
Bắt đầu từ llama_cpp/_ctypes_extensions.py và theo dõi load_shared_library, đặc biệt là các lệnh gọi os.add_dll_directory trên Windows và những lần cập nhật PATH trước khi tải bằng CDLL. Tái hiện bằng cách liên tục thử import nhưng thất bại trên Windows, rồi kiểm tra cách các handle thư mục DLL được quản lý khi việc tải phát sinh ngoại lệ. Hoàn tất khi các lần thử thất bại không làm thay đổi PATH và các thư mục tìm kiếm DLL, hoặc tránh thêm các mục trùng lặp.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
On Windows, load_shared_library in llama_cpp/_ctypes_extensions.py changes process state before it knows the library will load:
if sys.platform == "win32":
os.add_dll_directory(str(base_path))
os.environ["PATH"] = str(base_path) + os.pathsep + os.environ["PATH"]
if sys.platform == "win32" and sys.version_info >= (3, 8):
os.add_dll_directory(str(base_path))
if "CUDA_PATH" in os.environ:
os.add_dll_directory(os.path.join(os.environ["CUDA_PATH"], "bin"))
os.add_dll_directory(os.path.join(os.environ["CUDA_PATH"], "lib"))
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:
import os
for _ in range(3):
try:
import llama_cpp
except RuntimeError as error:
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_directoryreturns, and close them when theCDLLcall fails. - Prepend to PATH only if the entry is not already there.
base_pathis 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)
- Windows 11, Python 3.12
CUDA_PATHpoints to CUDA 13.1
- Ngôn ngữ chính
- Python
- Star
- 10.7k
- Fork
- 1.5k
- Merge trung bình
- 1 giờ 40 phút
- Pull request đã merge (30 ngày)
- 5
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của abetlen/llama-cpp-python
-
Seven llama_sampler_init_* bindings admit keyword arguments that the ctypes function object silently dropsCó thể đã có người làm @Belal0066 đã nhận 59 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
abetlen/llama-cpp-python#2371 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
uv add llama-cpp-python wheels fails for versions above 0.3.30Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
abetlen/llama-cpp-python#2352 · 1 bình luận · 3 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
abetlen/llama-cpp-python#2314 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Llama.embed() calls LlamaBatch.add_sequence with old 3-arg signature; missing logits_arrayCó thể làm lại được @lxcxjxhx đã nhận 136 ngày trước và không có pull request nào đang mở. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
abetlen/llama-cpp-python#2211 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Llama() silently accepts and discards `embedding` kwarg; .embed() then raises confusinglyCó thể đã có người làm @Anai-Guo đã nhận 76 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
abetlen/llama-cpp-python#2210 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của abetlen/llama-cpp-python
Issue tương tự
-
Bug IronPython Python 3
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
pyrevitlabs/pyRevit#4184 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
CLAW (Agents & Orchestration)Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[Plugin] 群聊记忆 · 人格养成Đang mở
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 74/100