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>
This commit is contained in:
千岚之夏
2026-06-10 15:31:48 +08:00
committed by GitHub
parent 60dfd565a6
commit bec0de2e2b
2 changed files with 16 additions and 9 deletions

View File

@@ -1,3 +1,9 @@
from .vec_db import FaissVecDB
def __getattr__(name: str):
if name == "FaissVecDB":
from .vec_db import FaissVecDB
return FaissVecDB
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
__all__ = ["FaissVecDB"]

View File

@@ -1,9 +1,3 @@
try:
import faiss
except ModuleNotFoundError:
raise ImportError(
"faiss 未安装。请使用 'pip install faiss-cpu''pip install faiss-gpu' 安装。",
)
import os
import numpy as np
@@ -11,6 +5,13 @@ import numpy as np
class EmbeddingStorage:
def __init__(self, dimension: int, path: str | None = None) -> None:
try:
import faiss
except ModuleNotFoundError as e:
raise ImportError(
"faiss 未安装。请使用 'pip install faiss-cpu''pip install faiss-gpu' 安装。",
) from e
self._faiss = faiss
self.dimension = dimension
self.path = path
self.index = None
@@ -67,7 +68,7 @@ class EmbeddingStorage:
"""
assert self.index is not None, "FAISS index is not initialized."
faiss.normalize_L2(vector)
self._faiss.normalize_L2(vector)
distances, indices = self.index.search(vector, k)
return distances, indices
@@ -92,4 +93,4 @@ class EmbeddingStorage:
"""
if self.index is None:
return
faiss.write_index(self.index, self.path)
self._faiss.write_index(self.index, self.path)