Skip to content

Commit 0171e0c

Browse files
authored
Merge pull request #1811 from danielaskdd/fix-keyed-lock
Fix: implemented entity-keyed locks for edge merging operations to ensure robust race condition protection
2 parents 66967c3 + cba97c6 commit 0171e0c

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

lightrag/operate.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ async def _locked_rebuild_relationship(src, tgt, chunk_ids):
432432
# Sort src and tgt to ensure order-independent lock key generation
433433
sorted_key_parts = sorted([src, tgt])
434434
async with get_storage_keyed_lock(
435-
f"{sorted_key_parts[0]}-{sorted_key_parts[1]}",
435+
sorted_key_parts,
436436
namespace=namespace,
437437
enable_logging=False,
438438
):
@@ -1115,23 +1115,18 @@ async def _merge_edges_then_upsert(
11151115
)
11161116

11171117
for need_insert_id in [src_id, tgt_id]:
1118-
workspace = global_config.get("workspace", "")
1119-
namespace = f"{workspace}:GraphDB" if workspace else "GraphDB"
1120-
async with get_storage_keyed_lock(
1121-
[need_insert_id], namespace=namespace, enable_logging=False
1122-
):
1123-
if not (await knowledge_graph_inst.has_node(need_insert_id)):
1124-
await knowledge_graph_inst.upsert_node(
1125-
need_insert_id,
1126-
node_data={
1127-
"entity_id": need_insert_id,
1128-
"source_id": source_id,
1129-
"description": description,
1130-
"entity_type": "UNKNOWN",
1131-
"file_path": file_path,
1132-
"created_at": int(time.time()),
1133-
},
1134-
)
1118+
if not (await knowledge_graph_inst.has_node(need_insert_id)):
1119+
await knowledge_graph_inst.upsert_node(
1120+
need_insert_id,
1121+
node_data={
1122+
"entity_id": need_insert_id,
1123+
"source_id": source_id,
1124+
"description": description,
1125+
"entity_type": "UNKNOWN",
1126+
"file_path": file_path,
1127+
"created_at": int(time.time()),
1128+
},
1129+
)
11351130

11361131
force_llm_summary_on_merge = global_config["force_llm_summary_on_merge"]
11371132

@@ -1287,7 +1282,7 @@ async def _locked_process_edges(edge_key, edges):
12871282
sorted_edge_key = sorted([edge_key[0], edge_key[1]])
12881283
# logger.info(f"Processing edge: {sorted_edge_key[0]} - {sorted_edge_key[1]}")
12891284
async with get_storage_keyed_lock(
1290-
f"{sorted_edge_key[0]}-{sorted_edge_key[1]}",
1285+
sorted_edge_key,
12911286
namespace=namespace,
12921287
enable_logging=False,
12931288
):

0 commit comments

Comments
 (0)