From 86656de1eddd226747115ce92d12ce6794803619 Mon Sep 17 00:00:00 2001 From: Eddie Cho Date: Wed, 16 Aug 2023 01:48:03 +0800 Subject: [PATCH] GH-8699: Fix Issue of removeLockKey() 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`** (cherry picked from commit ba6d35d123df0069d432956ee8d5020bca1ee826) --- .../redis/util/RedisLockRegistry.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java index 8c05ec41a3..ae4f68022a 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java @@ -465,13 +465,10 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl 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; @@ -484,6 +481,15 @@ public final class RedisLockRegistry implements ExpirableLockRegistry, Disposabl "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. " +