Skip to content

Commit bec0de2

Browse files
authored
fix: defer faiss C library import to prevent process hang on startup (#8696)
* fix: defer faiss C library import to prevent process hang on startup Move top-level `import faiss` from embedding_storage.py to __init__() method, and make the EmbeddingStorage import in vec_db.py lazy. This prevents the faiss C library from being loaded at module import time, which can cause process hang (SIGILL or deadlock) with faiss-cpu 1.14.2 on certain CPU architectures. Ref #8695 * chore: apply review suggestions - Preserve original exception context with `from e` when re-raising ImportError - Move EmbeddingStorage import back to top level in vec_db.py (safe now that faiss is no longer imported at module level in embedding_storage.py) * ci: trigger re-run --------- Co-authored-by: Blueteemo <Blueteemo@users.noreply.github.com>
1 parent 60dfd56 commit bec0de2

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
from .vec_db import FaissVecDB
1+
def __getattr__(name: str):
2+
if name == "FaissVecDB":
3+
from .vec_db import FaissVecDB
4+
5+
return FaissVecDB
6+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
7+
28

39
__all__ = ["FaissVecDB"]

astrbot/core/db/vec_db/faiss_impl/embedding_storage.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
try:
2-
import faiss
3-
except ModuleNotFoundError:
4-
raise ImportError(
5-
"faiss 未安装。请使用 'pip install faiss-cpu' 或 'pip install faiss-gpu' 安装。",
6-
)
71
import os
82

93
import numpy as np
104

115

126
class EmbeddingStorage:
137
def __init__(self, dimension: int, path: str | None = None) -> None:
8+
try:
9+
import faiss
10+
except ModuleNotFoundError as e:
11+
raise ImportError(
12+
"faiss 未安装。请使用 'pip install faiss-cpu' 或 'pip install faiss-gpu' 安装。",
13+
) from e
14+
self._faiss = faiss
1415
self.dimension = dimension
1516
self.path = path
1617
self.index = None
@@ -67,7 +68,7 @@ async def search(self, vector: np.ndarray, k: int) -> tuple:
6768
6869
"""
6970
assert self.index is not None, "FAISS index is not initialized."
70-
faiss.normalize_L2(vector)
71+
self._faiss.normalize_L2(vector)
7172
distances, indices = self.index.search(vector, k)
7273
return distances, indices
7374

@@ -92,4 +93,4 @@ async def save_index(self) -> None:
9293
"""
9394
if self.index is None:
9495
return
95-
faiss.write_index(self.index, self.path)
96+
self._faiss.write_index(self.index, self.path)

0 commit comments

Comments
 (0)