Fix "TryLock interrupted keeps renewing lock indefinitely"

Signed-off-by: César Luis Alvargonzález <cesar.alvargonzalez@revolut.com>
pull/3826/head
César Luis Alvargonzález 4 years ago
parent 28033956fc
commit 6153d05573

@ -174,7 +174,13 @@ public abstract class RedissonBaseLock extends RedissonExpirable implements RLoc
oldEntry.addThreadId(threadId); oldEntry.addThreadId(threadId);
} else { } else {
entry.addThreadId(threadId); entry.addThreadId(threadId);
renewExpiration(); try {
renewExpiration();
} finally {
if (Thread.currentThread().isInterrupted()) {
cancelExpirationRenewal(threadId);
}
}
} }
} }

@ -112,6 +112,7 @@ public class CommandAsyncService implements CommandAsyncExecutor {
try { try {
future.await(); future.await();
} catch (InterruptedException e) { } catch (InterruptedException e) {
future.cancel(true);
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
throw new RedisException(e); throw new RedisException(e);
} }

@ -111,6 +111,29 @@ public class RedissonLockTest extends BaseConcurrentTest {
runner.stop(); runner.stop();
} }
@Test
public void testLockIsNotRenewedAfterInterruptedTryLock() throws InterruptedException {
final CountDownLatch countDownLatch = new CountDownLatch(1);
RLock lock = redisson.getLock("myLock");
assertThat(lock.isLocked()).isFalse();
Thread thread = new Thread(() -> {
countDownLatch.countDown();
if (!lock.tryLock()) {
return;
}
lock.unlock();
});
thread.start();
countDownLatch.await();
// let the tcp request be sent out
TimeUnit.MILLISECONDS.sleep(5);
thread.interrupt();
TimeUnit.SECONDS.sleep(45);
assertThat(lock.isLocked()).isFalse();
}
@Test @Test
public void testRedisFailed() { public void testRedisFailed() {
Assertions.assertThrows(WriteRedisConnectionException.class, () -> { Assertions.assertThrows(WriteRedisConnectionException.class, () -> {

Loading…
Cancel
Save