Repository navigation
Description
Redis version
8.10 (Docker image redis:8.10)
Redisson version
4.8.0, the latest release, and master at 1d8129a. I saw the same with 3.20.1, 3.43.0 and 3.44.0. I did not test other versions.
Redisson configuration
Single server, everything at its default, except lockWatchdogTimeout, which is 3000 ms in the reproducer so that the effect shows quickly:
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
config.setLockWatchdogTimeout(3000);What is the Expected behavior?
A thread that is interrupted while tryLock() waits for the reply does not get the lock. When Redis has executed the script by then, the lock expires after lockWatchdogTimeout. The watchdog does not renew it.
RedissonLockTest#testLockIsNotRenewedAfterInterruptedTryLock asserts this.
What is the Actual behavior?
In some of these cases the watchdog renews the lock forever. The thread got an exception, so it does not unlock a lock it never got. isLocked() stays true, and remainTimeToLive() goes back to about 3000 ms again and again.
Reproducer, 5 attempts:
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
public class Reproducer {
public static void main(String[] args) throws Exception {
for (int i = 1; i <= 5; i++) {
// A new client each time. The first command of a client takes a few milliseconds.
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
config.setLockWatchdogTimeout(3000);
RedissonClient client = Redisson.create(config);
RLock lock = client.getLock("lock");
AtomicReference<String> outcome = new AtomicReference<>("returned false");
Thread thread = new Thread(() -> {
try {
if (lock.tryLock()) {
outcome.set("acquired and unlocked");
lock.unlock();
}
} catch (Throwable e) {
outcome.set("threw " + e);
}
});
thread.start();
TimeUnit.MILLISECONDS.sleep(5);
thread.interrupt();
thread.join();
// Without a renewal the lock expires after 3 s. The watchdog renews every 1 s.
TimeUnit.SECONDS.sleep(7);
System.out.println("run " + i + ": thread " + outcome.get()
+ ". 7 s later: isLocked=" + lock.isLocked() + ", remainTimeToLive=" + lock.remainTimeToLive());
lock.forceUnlock();
client.shutdown();
}
}
}One of my runs:
run 1: thread threw RedisException: java.lang.InterruptedException. 7 s later: isLocked=true, remainTimeToLive=2144
run 2: thread threw RedisException: java.lang.InterruptedException. 7 s later: isLocked=false, remainTimeToLive=-2
run 3: thread acquired and unlocked. 7 s later: isLocked=false, remainTimeToLive=-2
run 4: thread acquired and unlocked. 7 s later: isLocked=false, remainTimeToLive=-2
run 5: thread acquired and unlocked. 7 s later: isLocked=false, remainTimeToLive=-2
Whether an attempt leaks depends on timing. In 20 attempts the thread was interrupted 9 times, and 2 of them leaked. With getSpinLock instead of getLock I saw it in 1 of 5 attempts.
RedissonLockTest#testLockIsNotRenewedAfterInterruptedTryLock fails when it is run alone on master: 5 of 5 runs for me. In a fresh JVM the first acquisition takes longer than the 5 ms the test waits before it interrupts, so the interrupt comes first. In one run with other tests before it, it passed.
Additional information
This is not the case of #2714 and #4368, where the thread is interrupted after it got the lock, and where the watchdog does not watch threads. Here the thread never gets the lock.
I added prints to the renewal code. This is the order of events in a failing run on master:
- The thread calls
tryLock(), which isget(tryLockAsync()).tryLockAsync(long threadId)returns the future ofServiceManager.execute(...). That is a second future, which is completed from the attempt. - The thread is interrupted while it waits.
CommandAsyncService.getcancels that second future and throws. The cancel does not reach the attempt. - 2 ms later the reply arrives. The
thenApplyintryAcquireOnceAsyncruns, sees that the lock was acquired and schedules the renewal.
The first attempt of lock(), lockInterruptibly() and tryLock(waitTime, ...) goes through tryAcquireAsync in the same way. RSpinLock attaches the renewal to the acquire future with thenAccept, and the cancel does not reach that future either.
Activity
eatmoreduck commentedon Dec 1, 2026
I opened #7464 for this: the synchronous
tryLock(), and the first attempt of the synchronouslock(),lockInterruptibly()andtryLock(waitTime, ...), schedule the renewal after they have received the result. The asynchronous methods are not changed.Fixed - lock watchdog renews a lock forever after an interrupted tryL…
Merge pull request #7464 from eatmoreduck/fix/interrupted-trylock-ren…