Skip to content

fix: lazy import faiss in embedding_storage.py#9350

Open
irmia2026 wants to merge 1 commit into
AstrBotDevs:masterfrom
irmia2026:fix/faiss-lazy-import-regression
Open

fix: lazy import faiss in embedding_storage.py#9350
irmia2026 wants to merge 1 commit into
AstrBotDevs:masterfrom
irmia2026:fix/faiss-lazy-import-regression

Conversation

@irmia2026

@irmia2026 irmia2026 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

采用与 #8695 一致的懒加载方式处理 embedding_storage.py 中的 import faiss

Changes

import faiss 从模块顶层移至 __init__() 和方法内部,避免 faiss-cpu 1.14.2 C 库在模块加载时触发 CPU 特性检测死锁。

Location Before After
Module top import faiss 移除;TYPE_CHECKING 守卫类型注解
__init__() 直接使用 faiss. import faiss 在 try/except 内
_read_index() 直接使用 faiss. 方法内 import faiss
_write_index() 直接使用 faiss. 方法内 import faiss

ASCII 临时文件桥接逻辑(_needs_bridge_safe_temp_dir_make_temp_file)保持不变。

Related

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@irmia2026 irmia2026 closed this Jul 22, 2026
@irmia2026
irmia2026 deleted the fix/faiss-lazy-import-regression branch July 22, 2026 08:31
@irmia2026
irmia2026 restored the fix/faiss-lazy-import-regression branch July 23, 2026 04:35
@irmia2026 irmia2026 reopened this Jul 23, 2026
@irmia2026
irmia2026 marked this pull request as ready for review July 23, 2026 04:35
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 23, 2026
@irmia2026

irmia2026 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Reopened as follow-up per discussion in #8323. Ready for review. 🙏

@dosubot dosubot Bot added the area:core The bug / feature is about astrbot's core, backend label Jul 23, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The lazy import faiss pattern is now duplicated in __init__ and implicitly relied on in the static methods; consider centralizing it in a small helper (e.g., _get_faiss() that handles ImportError) to keep behavior and error messaging consistent.
  • If _read_index and _write_index are intended to be usable without instantiating EmbeddingStorage, you may want to add explicit ImportError handling in those methods similar to __init__ so callers see a clear, localized error instead of a generic module import failure.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The lazy `import faiss` pattern is now duplicated in `__init__` and implicitly relied on in the static methods; consider centralizing it in a small helper (e.g., `_get_faiss()` that handles ImportError) to keep behavior and error messaging consistent.
- If `_read_index` and `_write_index` are intended to be usable without instantiating `EmbeddingStorage`, you may want to add explicit ImportError handling in those methods similar to `__init__` so callers see a clear, localized error instead of a generic module import failure.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

 regression)

PR AstrBotDevs#8323 inadvertently moved import faiss back to module top level,
re-introducing the faiss-cpu 1.14.2 deadlock fixed by PR AstrBotDevs#8696.

This commit moves the import back inside __init__() (as AstrBotDevs#8696 did),
with local imports in _read_index/_write_index static methods.
All ASCII temp-file bridge logic from AstrBotDevs#8323 is preserved.
@irmia2026
irmia2026 force-pushed the fix/faiss-lazy-import-regression branch from 7717e7d to 06eb41e Compare July 23, 2026 04:40
@irmia2026 irmia2026 changed the title fix: restore lazy faiss import to prevent process hang (#8695 regression) fix: lazy import faiss in embedding_storage.py Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant