Skip to content

Build and run agents you can see, understand and trust.

fix(rag): serialize knowledge base collection initialization (#2853)

SIcheng Han92535e3da401
diff --git a/src/agentscope/rag/_knowledge.py b/src/agentscope/rag/_knowledge.py
index 83451331a..8534fadb7 100644
--- a/src/agentscope/rag/_knowledge.py
+++ b/src/agentscope/rag/_knowledge.py
@@ -128,6 +128,7 @@ class KnowledgeBase:
# successful ensure_collection so subsequent operations avoid
# the extra round-trip.
self._collection_ready = False
+ self._collection_lock = asyncio.Lock()
# ------------------------------------------------------------------
# Read-only accessors
@@ -176,12 +177,16 @@ class KnowledgeBase:
"""
if self._collection_ready:
return
- if not await self._vector_store.has_collection(self._collection):
- await self._vector_store.create_collection(
- self._collection,
- dimensions=self._embedding_model.dimensions,
- )
- self._collection_ready = True
+ async with self._collection_lock:
+ # Another caller may have initialized while we waited for I/O.
+ if self._collection_ready:
+ return
+ if not await self._vector_store.has_collection(self._collection):
+ await self._vector_store.create_collection(
+ self._collection,
+ dimensions=self._embedding_model.dimensions,
+ )
+ self._collection_ready = True
# ------------------------------------------------------------------
# Search