fix: add thread-safe write lock to ChromaDB operations

This commit is contained in:
2026-07-05 01:34:36 +08:00
parent 10eda446ea
commit 81a8173a38
3 changed files with 35 additions and 24 deletions
+13 -11
View File
@@ -206,12 +206,13 @@ class DocumentIngestor:
for i, c in enumerate(chunks)
]
self.collection.add(
ids=ids,
embeddings=embeddings,
documents=texts,
metadatas=metadatas,
)
with self.db.write_lock:
self.collection.add(
ids=ids,
embeddings=embeddings,
documents=texts,
metadatas=metadatas,
)
return len(chunks)
@@ -226,10 +227,11 @@ class DocumentIngestor:
def _remove_by_source(self, file_name: str) -> None:
"""按 source_file 删除已有 chunks."""
try:
existing = self.collection.get(
where={"source_file": file_name}
)
if existing and existing["ids"]:
self.collection.delete(ids=existing["ids"])
with self.db.write_lock:
existing = self.collection.get(
where={"source_file": file_name}
)
if existing and existing["ids"]:
self.collection.delete(ids=existing["ids"])
except Exception:
pass # collection 为空时 get 可能抛异常