Repository navigation
Description
Filed by Garvit's Claude agent (Claude Code). The analysis and every measurement below were made by the agent on 30 November 2026. The reproducer is at the end.
Redis version
8.12.0, standalone
Redisson version
4.8.0. Also reproduced on 4.0.0, 4.3.0, 4.5.0, 4.6.1, 4.7.0 and on master at b019f0e.
Redisson configuration
Single server, everything at its default:
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");The 45 second runs lower lockWatchdogTimeout to 1500 so that an unrenewed lock expires quickly. The default-settings run keeps 30000.
What is the Expected behavior?
A lock taken without a leaseTime is renewed for as long as its owner holds it. No other client can take it in that time.
What is the Actual behavior?
When a thread releases a lock and takes the same lock again right away, the new hold is sometimes no longer registered for renewal. Nothing is logged. If that hold lasts longer than lockWatchdogTimeout, the key expires, another client takes the lock, and both run at the same time. The first owner finds out only when it calls unlock():
java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 54d2626c-f4a6-4d52-8317-33b8fe279b96 thread-id: 3
Default settings
Redisson 4.8.0, lockWatchdogTimeout 30000. Client one has one thread that does lock(), 1 ms of work, unlock(), and then lock() again at once. After each lock() the reproducer looks whether Redisson still has the lock registered for renewal. When it has not, client one keeps holding the lock and client two starts calling tryLock().
In 150 seconds and 24,516 lock cycles, 4 holds were not registered for renewal, the first one at cycle 5,749. Client two acquired the lock in 4 of them: 30,012 ms, 30,005 ms, 30,006 ms and 30,011 ms into client one's hold. Client one had not released it, and got the exception above when it did.
Every 4.x release tried, and master
The same reproducer with lockWatchdogTimeout 1500, for 45 seconds.
| Redisson | lock cycles | holds that lost their renewal | of those, taken by client two while client one still held |
|---|---|---|---|
| 4.0.0 | 6,989 | 21 | 21 |
| 4.3.0 | 8,969 | 21 | 21 |
| 4.5.0 | 8,974 | 20 | 20 |
| 4.6.1 | 8,930 | 20 | 20 |
| 4.7.0 | 8,336 | 21 | 21 |
| 4.8.0 | 8,688 | 21 | 21 |
master (b019f0e) |
6,877 | 21 | 21 |
It needs the same lock name and the same thread
Redisson 4.8.0, lockWatchdogTimeout 1500, one thread, 1 ms of work per hold, 45 seconds, counting holds that are not registered for renewal:
| each cycle uses | lock cycles | holds that lost their renewal |
|---|---|---|
| the same lock name on the same thread | 27,533 | 225 |
| a lock name never used before | 27,676 | 0 |
| a thread never used before | 27,182 | 0 |
This variant does not stop at a loss to wait for client two, which is why the count in its first row is higher than in the table above.
#7454 makes it frequent, but it does not depend on #7454
Every extra renewal task from #7454 is one more renewal script that can land between an unlock() and the next lock(), so that issue raises the rate a lot. One renewal task is enough, though. Redisson 4.8.0 with the fix suggested in #7454 applied:
| run, with the #7454 fix applied | lock cycles | holds that lost their renewal |
|---|---|---|
the reproducer as above, lockWatchdogTimeout 1500, 45 seconds |
22,427 | 1, taken by client two while client one still held it |
lockWatchdogTimeout 300, 8 threads that each re-take their own lock, 1 ms of work, 180 seconds |
891,132 | 312 |
The same 8-thread run without that fix, for 60 seconds: 1,322 lost in 296,458 lock cycles.
Additional information
Cause
LockTask.buildChunk sends one renewal script for a chunk of locks. For each key it passes the lock name of the thread that was registered when the chunk was built. When the script reports a key as not held, the callback cancels the renewal of that key for that thread id.
LockTask.java#L97-L102 (the file is identical in 4.8.0):
return new ChunkExecution<>(f, existingNames -> {
keys.removeAll(existingNames);
for (String key : keys) {
cancelExpirationRenewal(key, name2threadId.get(key));
}
});The result only says that the lock was not held at the moment the script ran. By the time the callback runs, the same thread can hold the lock again:
- Thread T holds the lock (hold 1). A renewal task fires, and
buildChunksends the script with the key and the lock name of T. - T calls
unlock(). The unlock script runs in Redis, andunlockAsync0removes the renewal entry. - The renewal script of step 1 runs in Redis after the unlock script and before the next lock script.
hexistsis 0, so it reports the key as not held. - T calls
lock()again (hold 2).tryAcquireAsynccallsscheduleExpirationRenewal, which adds a new entry for the key and T. - The callback of step 1 runs.
cancelExpirationRenewal(key, T)finds the entry of hold 2, removes T from it and drops the entry.
Hold 2 is never renewed, and the caller is not told.
RenewalTask.cancelExpirationRenewal removes by lock name and thread id only, so it cannot tell hold 1 from hold 2. That is why both have to be the same, and why a new lock name or a new thread per cycle shows no loss.
The callbacks in ReadLockTask and FastMultilockTask cancel in the same way. Only RLock was measured.
Suggested fix
Tie the cancel to the registration that the result was computed for, so that a result for an earlier hold cannot remove a later one. One way: give every registration a number from a client-wide counter, keep it in LockEntry per thread id, read it in buildChunk next to the thread id, and let the callback remove the thread id only if the entry still carries that number. A counter that lives inside one LockEntry is not enough, because the re-lock creates a new LockEntry that starts from the same value.
This fix has not been built or tested.
Related issues
- Lock watchdog: renewal tasks multiply each time the set of renewed locks goes empty and non-empty again #7454 (renewal tasks multiply) is a separate defect. It raises the rate of this one, as measured above.
- Attempt to unlock lock, not locked by current thread by node id in redisson 3.52 #6844 reported the same
IllegalMonitorStateExceptionon 3.52 and was closed with Fix RLock trylock and unlock race condition #7070 (dc4cdf0), which madeadd()andcancelExpirationRenewal()atomic with each other. That commit is in 4.4.0 and later. It does not cover a cancel that arrives with a stale result, and the runs above on 4.5.0 tomasterinclude it. - Watchdog doesn't renew RLock intermittently which leads to premature release of RLock #5698 described the same symptom on 3.27.2, before the renewal tasks of today were introduced with 597d604 in 3.44.0. No 3.x release was measured here.
- Watchdog renewal can outlive unlock(): cancelLockRenewal races the acquire's async scheduleExpirationRenewal #7272 is a different race: a renewal that keeps running after
unlock().
Reproducer
RenewalLost.java
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
/**
* Client one: one thread takes one watchdog lock, holds it for 1 ms, releases it and takes it again
* at once. After each acquire it looks whether Redisson still has the lock registered for renewal.
* When it has not, client one keeps holding the lock and client two tries to take it.
*
* args: [lockWatchdogTimeout ms, default 1500] [seconds, default 45]
*/
public class RenewalLost {
public static void main(String[] args) throws Exception {
long watchdog = args.length > 0 ? Long.parseLong(args[0]) : 1500;
int seconds = args.length > 1 ? Integer.parseInt(args[1]) : 45;
Config config = new Config();
config.useSingleServer().setAddress(System.getProperty("redis", "redis://127.0.0.1:6379"));
config.setLockWatchdogTimeout(watchdog);
RedissonClient one = Redisson.create(config);
RedissonClient two = Redisson.create(config);
String name = "renewal-lost";
RLock lock = one.getLock(name);
RLock other = two.getLock(name);
Map<?, ?> renewed = null;
long cycles = 0;
long lost = 0;
long twoHolders = 0;
long end = System.currentTimeMillis() + seconds * 1000L;
while (System.currentTimeMillis() < end) {
lock.lock();
cycles++;
Thread.sleep(1);
if (renewed == null) {
renewed = renewalMap(lock);
}
if (!renewed.containsKey(name)) {
lost++;
long start = System.currentTimeMillis();
boolean second = false;
while (!second && System.currentTimeMillis() - start < watchdog + 3000) {
second = other.tryLock(0, 2000, TimeUnit.MILLISECONDS);
if (!second) {
Thread.sleep(25);
}
}
System.out.println("cycle " + cycles + ": client one holds the lock, but it is not registered for renewal. "
+ (second ? "Client two acquired it " : "Client two did not acquire it within ")
+ (System.currentTimeMillis() - start) + " ms into that hold.");
if (second) {
twoHolders++;
other.unlock();
}
}
try {
lock.unlock();
} catch (IllegalMonitorStateException e) {
System.out.println(" client one unlock: " + e.getMessage());
}
}
System.out.println("lock cycles=" + cycles + ", holds that lost their renewal=" + lost
+ ", of those taken by client two while client one still held=" + twoHolders);
one.shutdown();
two.shutdown();
}
/** The lock names Redisson renews: RedissonBaseLock.renewalScheduler -> reference (LockTask) -> name2entry. */
static Map<?, ?> renewalMap(RLock lock) throws Exception {
Field scheduler = Class.forName("org.redisson.RedissonBaseLock").getDeclaredField("renewalScheduler");
scheduler.setAccessible(true);
Object renewalScheduler = scheduler.get(lock);
Field reference = renewalScheduler.getClass().getDeclaredField("reference");
reference.setAccessible(true);
Object task = ((AtomicReference<?>) reference.get(renewalScheduler)).get();
Field map = task.getClass().getSuperclass().getDeclaredField("name2entry");
map.setAccessible(true);
return (Map<?, ?>) map.get(task);
}
}java RenewalLost # lockWatchdogTimeout 1500, 45 seconds
java RenewalLost 30000 150 # default watchdog, 150 seconds
java -Dredis=redis://host:port RenewalLost
It reads RenewalTask.name2entry by reflection, only to see the loss right after the lock() call instead of waiting for the key to expire. The second client then shows the effect without any reflection.
The table under "It needs the same lock name and the same thread" and the runs with the fix for #7454 used small variants of this program. They can be posted if they are useful.
Raw output
45 second runs, per version (first lines and the summary line of each)
===== Redisson 4.0.0 =====
cycle 243: client one holds the lock, but it is not registered for renewal. Client two acquired it 1515 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: ac332ffa-0418-40ed-8481-5ceca0e76024 thread-id: 3
cycle 547: client one holds the lock, but it is not registered for renewal. Client two acquired it 1515 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: ac332ffa-0418-40ed-8481-5ceca0e76024 thread-id: 3
...
lock cycles=6989, holds that lost their renewal=21, of those taken by client two while client one still held=21
===== Redisson 4.3.0 =====
cycle 233: client one holds the lock, but it is not registered for renewal. Client two acquired it 1508 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 1a0ec196-627b-4cee-a3ba-63ad525404f4 thread-id: 3
cycle 532: client one holds the lock, but it is not registered for renewal. Client two acquired it 1504 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 1a0ec196-627b-4cee-a3ba-63ad525404f4 thread-id: 3
...
lock cycles=8969, holds that lost their renewal=21, of those taken by client two while client one still held=21
===== Redisson 4.5.0 =====
cycle 471: client one holds the lock, but it is not registered for renewal. Client two acquired it 1515 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 71b0dfcb-f468-45ea-a1c1-75cb6651dfdb thread-id: 3
cycle 796: client one holds the lock, but it is not registered for renewal. Client two acquired it 1503 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 71b0dfcb-f468-45ea-a1c1-75cb6651dfdb thread-id: 3
...
lock cycles=8974, holds that lost their renewal=20, of those taken by client two while client one still held=20
===== Redisson 4.6.1 =====
cycle 443: client one holds the lock, but it is not registered for renewal. Client two acquired it 1521 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 1d3dbd12-5607-4763-a5a3-dd89bf4f217f thread-id: 3
cycle 883: client one holds the lock, but it is not registered for renewal. Client two acquired it 1514 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 1d3dbd12-5607-4763-a5a3-dd89bf4f217f thread-id: 3
...
lock cycles=8930, holds that lost their renewal=20, of those taken by client two while client one still held=20
===== Redisson 4.7.0 =====
cycle 269: client one holds the lock, but it is not registered for renewal. Client two acquired it 1513 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 83c3871b-c1d3-4227-9b1b-94d5483350e4 thread-id: 3
cycle 621: client one holds the lock, but it is not registered for renewal. Client two acquired it 1507 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 83c3871b-c1d3-4227-9b1b-94d5483350e4 thread-id: 3
...
lock cycles=8336, holds that lost their renewal=21, of those taken by client two while client one still held=21
===== Redisson 4.8.0 =====
cycle 307: client one holds the lock, but it is not registered for renewal. Client two acquired it 1508 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 11f941c4-a431-4f52-95ee-27d23f5ff5a6 thread-id: 3
cycle 706: client one holds the lock, but it is not registered for renewal. Client two acquired it 1513 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 11f941c4-a431-4f52-95ee-27d23f5ff5a6 thread-id: 3
...
lock cycles=8688, holds that lost their renewal=21, of those taken by client two while client one still held=21
===== Redisson master b019f0e =====
cycle 748: client one holds the lock, but it is not registered for renewal. Client two acquired it 1513 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: aad86781-132d-4390-a74a-7723cbcd82b0 thread-id: 3
cycle 1068: client one holds the lock, but it is not registered for renewal. Client two acquired it 1525 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: aad86781-132d-4390-a74a-7723cbcd82b0 thread-id: 3
...
lock cycles=6877, holds that lost their renewal=21, of those taken by client two while client one still held=21
Default-settings run, Redisson 4.8.0
cycle 5749: client one holds the lock, but it is not registered for renewal. Client two acquired it 30012 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 54d2626c-f4a6-4d52-8317-33b8fe279b96 thread-id: 3
cycle 11583: client one holds the lock, but it is not registered for renewal. Client two acquired it 30005 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 54d2626c-f4a6-4d52-8317-33b8fe279b96 thread-id: 3
cycle 17909: client one holds the lock, but it is not registered for renewal. Client two acquired it 30006 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 54d2626c-f4a6-4d52-8317-33b8fe279b96 thread-id: 3
cycle 24516: client one holds the lock, but it is not registered for renewal. Client two acquired it 30011 ms into that hold.
client one unlock: attempt to unlock lock, not locked by current thread by node id: 54d2626c-f4a6-4d52-8317-33b8fe279b96 thread-id: 3
lock cycles=24516, holds that lost their renewal=4, of those taken by client two while client one still held=4
Test machine: macOS 27.2.1 on arm64, OpenJDK 25.0.5.1 (Temurin), Redis 8.12.0 on localhost.
Activity
eatmoreduck commentedon Dec 1, 2026
Reproduced this on
masterat b019f0e and traced which cancel drops the new hold. I'm working on a fix and will link a PR from here.With your
RenewalLostatlockWatchdogTimeout1500 for 45 seconds (macOS arm64, OpenJDK 25, Redis 8.10.2), three runs lost 18, 20 and 18 registrations in 8.8k, 7.5k and 9.2k lock cycles. Two of those runs used a build with a temporary trace incancelExpirationRenewal. In the traced run every loss has the sequence you describe: the thread registers a new hold, and the callback of a renewal sent before the unlock then removes it. The cancel that followsunlock()did not remove a newer registration there.The fix follows your suggestion: each registration gets an id, the renewal result carries the ids it was computed for, and the callback only cancels a registration whose id still matches.
ReadLockTaskandFastMultilockTaskcancel from a result callback in the same way, so I am covering them too.Fixed - Lock watchdog stops renewing a hold retaken right after unlock …
Fixed - every cancel names the registration it removes redisson#7455
Fixed - keep renewal registrations distinct across cancels redisson#7455