feat: Ensure thread safety for graph write operations

Add a lock to delete, adelete_by_entity, and adelete_by_relation methods to prevent race conditions and ensure data consistency during concurrent modifications to the knowledge graph.
This commit is contained in:
yangdx 2025-06-23 09:57:56 +08:00
parent c947b20bb1
commit 9fae0eadff

View file

@ -35,6 +35,7 @@ from lightrag.kg import (
from lightrag.kg.shared_storage import ( from lightrag.kg.shared_storage import (
get_namespace_data, get_namespace_data,
get_pipeline_status_lock, get_pipeline_status_lock,
get_graph_db_lock,
) )
from .base import ( from .base import (
@ -1728,6 +1729,10 @@ class LightRAG:
relationships_to_delete = set() relationships_to_delete = set()
relationships_to_rebuild = {} # (src, tgt) -> remaining_chunk_ids relationships_to_rebuild = {} # (src, tgt) -> remaining_chunk_ids
# Use graph database lock to ensure atomic merges and updates
graph_db_lock = get_graph_db_lock(enable_logging=False)
async with graph_db_lock:
# Process entities # Process entities
all_labels = await self.chunk_entity_relation_graph.get_all_labels() all_labels = await self.chunk_entity_relation_graph.get_all_labels()
for node_label in all_labels: for node_label in all_labels:
@ -1857,6 +1862,9 @@ class LightRAG:
""" """
from .utils_graph import adelete_by_entity from .utils_graph import adelete_by_entity
# Use graph database lock to ensure atomic merges and updates
graph_db_lock = get_graph_db_lock(enable_logging=False)
async with graph_db_lock:
return await adelete_by_entity( return await adelete_by_entity(
self.chunk_entity_relation_graph, self.chunk_entity_relation_graph,
self.entities_vdb, self.entities_vdb,
@ -1877,6 +1885,9 @@ class LightRAG:
""" """
from .utils_graph import adelete_by_relation from .utils_graph import adelete_by_relation
# Use graph database lock to ensure atomic merges and updates
graph_db_lock = get_graph_db_lock(enable_logging=False)
async with graph_db_lock:
return await adelete_by_relation( return await adelete_by_relation(
self.chunk_entity_relation_graph, self.chunk_entity_relation_graph,
self.relationships_vdb, self.relationships_vdb,