Add atomic Redis lock acquisition to DistributedLockService#951
Merged
RUKAYAT-CODER merged 1 commit intoJun 29, 2026
Merged
Conversation
acquireLock already used a single SET key value NX PX command, but it returned a boolean and a hardcoded value, so any caller could release a lock it never held once the TTL expired. acquireLock now returns a unique token (or null if already held), releaseLock only deletes the key if the token still matches (atomic GET+DEL via Lua), and a new withLock(key, ttl, fn) helper acquires, runs fn, and always releases — even if fn throws. Closes rinafcode#888
|
@aliyudotdev Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
Thank you for contributing to the project. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
acquireLockalready issued a singleSET key value NX PX ttlcommand, but returned a plain boolean and wrote a hardcoded value ('locked'), so once the TTL expired any caller could release a lock it never held.acquireLocknow generates a unique token per call and returns it (ornullif the lock is already held).releaseLock(key, token)only deletes the key if the stored value still matches the token, via a single atomic LuaGET+DELscript — matching the pattern already used insession.service.ts.withLock(key, ttl, fn): acquires, runsfn, and always releases in afinallyblock, even iffnthrows.SET NX PXverified via spy, exactly one winner under 100 concurrentacquireLockcalls, token-mismatch release is a no-op, andwithLockreleases on both success and throw.Test plan
npm test -- distributed-lock.servicesetmock is called exactly once peracquireLockwith('key', token, 'PX', ttl, 'NX')acquireLockcalls on the same key returns a non-null tokenCloses #888