Skip to content

Commit

Permalink
GH-8699: Fix Issue of removeLockKey()
Browse files Browse the repository at this point in the history
In the current code, an `IllegalStateException` might be thrown from the try block while invoking the `removeLockKeyInnerUnlink()` method, especially when caused by key expiration (resulting in `unlinkResult == false`).

This triggers the check block, which incorrectly sets the `unlinkAvailable` flag to `false`, even if the Redis server supports the unlink operation.

As a consequence, the subsequent `removeLockKeyInnerDelete()` method is invoked when it should not be.

* `IllegalStateException` should not be thrown from try block
* Add a comment and fix Checkstyle violations

**Cherry-pick to `6.1.x` & `6.0.x`**
  • Loading branch information
EddieChoCho authored Aug 15, 2023
1 parent 0450a32 commit ba6d35d
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,13 +476,10 @@ public final void unlock() {

private void removeLockKey() {
if (RedisLockRegistry.this.unlinkAvailable) {
Boolean unlinkResult = null;
try {
boolean unlinkResult = removeLockKeyInnerUnlink();
if (!unlinkResult) {
throw new IllegalStateException("Lock was released in the store due to expiration. " +
"The integrity of data protected by this lock may have been compromised.");
}
return;
// Attempt to UNLINK the lock key; an exception indicates lack of UNLINK support
unlinkResult = removeLockKeyInnerUnlink();
}
catch (Exception ex) {
RedisLockRegistry.this.unlinkAvailable = false;
Expand All @@ -495,6 +492,15 @@ private void removeLockKey() {
"falling back to the regular DELETE command: " + ex.getMessage());
}
}

if (Boolean.TRUE.equals(unlinkResult)) {
// Lock key successfully unlinked
return;
}
else if (Boolean.FALSE.equals(unlinkResult)) {
throw new IllegalStateException("Lock was released in the store due to expiration. " +
"The integrity of data protected by this lock may have been compromised.");
}
}
if (!removeLockKeyInnerDelete()) {
throw new IllegalStateException("Lock was released in the store due to expiration. " +
Expand Down

0 comments on commit ba6d35d

Please sign in to comment.