Skip to content

Commit e8383df

Browse files
committed
Fix NamespaceLock context variable timing to prevent lock bricking
* Acquire lock before setting ContextVar * Prevent state corruption on cancellation * Fix permanent lock brick scenario * Store context only after success * Handle acquisition failure properly
1 parent 95e1fb1 commit e8383df

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lightrag/kg/shared_storage.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,9 +1514,12 @@ async def __aenter__(self):
15141514
enable_logging=self._enable_logging,
15151515
)
15161516

1517-
# Store context in this coroutine's ContextVar
1517+
# Acquire the lock first, then store context only after successful acquisition
1518+
# This prevents the ContextVar from being set if acquisition fails (e.g., due to cancellation),
1519+
# which would permanently brick the lock
1520+
result = await ctx.__aenter__()
15181521
self._ctx_var.set(ctx)
1519-
return await ctx.__aenter__()
1522+
return result
15201523

15211524
async def __aexit__(self, exc_type, exc_val, exc_tb):
15221525
"""Exit the current context and clean up"""

0 commit comments

Comments
 (0)