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
(cherry picked from commit e8383df3b8)
This commit is contained in:
parent
87561f8b28
commit
dc4c10c346
1 changed files with 5 additions and 2 deletions
|
|
@ -1503,9 +1503,12 @@ class NamespaceLock:
|
|||
enable_logging=self._enable_logging,
|
||||
)
|
||||
|
||||
# Store context in this coroutine's ContextVar
|
||||
# Acquire the lock first, then store context only after successful acquisition
|
||||
# This prevents the ContextVar from being set if acquisition fails (e.g., due to cancellation),
|
||||
# which would permanently brick the lock
|
||||
result = await ctx.__aenter__()
|
||||
self._ctx_var.set(ctx)
|
||||
return await ctx.__aenter__()
|
||||
return result
|
||||
|
||||
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
||||
"""Exit the current context and clean up"""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue