Skip to content

Fix premature lock status mutation in RedisLockRepository.AcquireAsync#35

Merged
Sarmkadan merged 1 commit into
mainfrom
fix/redis-lock-status-after-acquire
May 21, 2026
Merged

Fix premature lock status mutation in RedisLockRepository.AcquireAsync#35
Sarmkadan merged 1 commit into
mainfrom
fix/redis-lock-status-after-acquire

Conversation

@Sarmkadan

Copy link
Copy Markdown
Owner

Fixes #32

Bug: @lock.Status was set to Acquired before the StringSetAsync(... When.NotExists) call. On failed acquisition, the lock object had Status=Acquired despite not owning the lock.

Fix:

  1. Serialize lock with current (pending) status for the initial SET NX
  2. Only set Status=Acquired after confirming success == true
  3. Re-serialize and update the Redis entry with the correct status via When.Exists

The extra SET (with When.Exists) ensures the stored lock value in Redis reflects the actual acquired status, which matters for monitoring tools that inspect lock entries directly.

Move Status=Acquired assignment to after StringSetAsync confirms
success. Previously the status was set before the Redis SET NX
operation, leaving the lock object in Acquired state even when
acquisition failed. Also update the serialized value in Redis to
reflect the correct status.

Closes #32

@Sarmkadan Sarmkadan left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is correct. The second StringSetAsync with When.Exists is a nice touch - ensures Redis and the in-memory object are consistent. The race window between SET NX and the update SET is negligible since the lock is already held.

@Sarmkadan
Sarmkadan merged commit a56f287 into main May 21, 2026

@Sarmkadan Sarmkadan left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is correct. The second StringSetAsync with When.Exists is a nice touch - ensures Redis and the in-memory object are consistent. The race window between SET NX and the update SET is negligible since the lock is already held.

Sarmkadan added a commit that referenced this pull request Jul 11, 2026
)

Move Status=Acquired assignment to after StringSetAsync confirms
success. Previously the status was set before the Redis SET NX
operation, leaving the lock object in Acquired state even when
acquisition failed. Also update the serialized value in Redis to
reflect the correct status.

Closes #32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RedisLockRepository.AcquireAsync sets Status before checking if lock was acquired

1 participant