Add lock to protect file write operations in NanoVectorDBStorage

- Introduce asyncio.Lock for save operations
- Ensure thread-safe file writes
This commit is contained in:
yangdx
2025-02-01 10:14:59 +08:00
parent 6a326e2783
commit 635d4fd9e4

View File

@@ -76,6 +76,8 @@ class NanoVectorDBStorage(BaseVectorStorage):
cosine_better_than_threshold: float = float(os.getenv("COSINE_THRESHOLD", "0.2"))
def __post_init__(self):
# Initialize lock only for file operations
self._save_lock = asyncio.Lock()
# Use global config value if specified, otherwise use default
config = self.global_config.get("vector_db_storage_cls_kwargs", {})
self.cosine_better_than_threshold = config.get(
@@ -210,4 +212,6 @@ class NanoVectorDBStorage(BaseVectorStorage):
logger.error(f"Error deleting relations for {entity_name}: {e}")
async def index_done_callback(self):
self._client.save()
# Protect file write operation
async with self._save_lock:
self._client.save()