Repository navigation
Description
Redis version
8.2.2
Redisson version
4.4.0 (also present in 3.51.0 - same renewal design)
Redisson configuration
Single server, watchdog enabled (lockWatchdogTimeout default 30000; the reproducer lowers it to 1000 to see the leak faster), default codec, JDK 21.
What is the Expected behavior?
Once a lock is released, its watchdog must stop renewing the key. An acquire followed by a release must never leave a key whose TTL keeps getting refreshed.
What is the Actual behavior?
A watchdog lock sometimes stays in Redis with its TTL refreshed indefinitely (PTTL keeps jumping back up to lockWatchdogTimeout) after the holder released it and its thread is gone. It is intermittent.
Additional information
Root cause
scheduleExpirationRenewal runs from an async callback on the acquire future, and cancelExpirationRenewal runs from an async callback on the unlock future. Nothing orders the two, so the cancel can run before the schedule.
RedissonLock.tryAcquireAsync schedules renewal from a thenApply on the acquire future (and only for the watchdog case, leaseTime <= 0):
ttlRemainingFuture = handleNoSync(threadId, ttlRemainingFuture);
CompletionStage<Long> f = ttlRemainingFuture.thenApply(ttlRemaining -> {
if (ttlRemaining == null) {
if (leaseTime > 0) {
internalLockLeaseTime = unit.toMillis(leaseTime);
} else {
scheduleExpirationRenewal(threadId);
}
}
return ttlRemaining;
});RedissonBaseLock.unlockAsync0 cancels renewal from the unlock's handle:
CompletionStage<Void> f = future.handle((res, e) -> {
cancelExpirationRenewal(threadId, res);
...
});Both go through org.redisson.renewal.LockRenewalScheduler:
protected void scheduleExpirationRenewal(long threadId) {
renewalScheduler.renewLock(getRawName(), threadId, getLockName(threadId));
}
protected void cancelExpirationRenewal(Long threadId, Boolean unlockResult) {
renewalScheduler.cancelLockRenewal(getRawName(), threadId);
}renewLock adds the lock to RenewalTask.name2entry and arms renewal; cancelLockRenewal removes it. Renewal continues while the lock is in name2entry.
The bad interleaving:
- The acquire
SETis dispatched. ThethenApplyhas not run yet, so the lock is not inname2entry. - The unlock runs
cancelLockRenewal, which finds the lock is not registered and does nothing. (The unlockDELmay also reach Redis before the acquireSET- they can use different pooled connections - so it finds nothing to delete.) - The acquire's
thenApplyrunsscheduleExpirationRenewal->renewLock, registering the lock and arming renewal. - No cancel is left to undo it, so the key is renewed indefinitely.
The cancel is lost because it runs before the renewLock it was meant to undo.
How we hit it
We use the blocking lockInterruptibly() and release in a finally. Normally the synchronous acquire only returns after the thenApply has run, so the release's cancel is safely ordered after the schedule and there is no race. But when the acquiring thread is interrupted, the acquire returns before the thenApply runs, and the release in the finally then races the still-pending acquire. Interrupting a blocking lockInterruptibly() and releasing in a finally is ordinary, correct code, and it leaks the watchdog.
Interruption is only how we trigger it. The underlying problem is that renewLock and cancelLockRenewal are not ordered.
Acquiring with an explicit positive lease (lockInterruptibly(leaseTime, unit)) never leaks, because scheduleExpirationRenewal is not called for a fixed lease. That confirms the diagnosis.
Reproducer
Needs a running Redis, plus redisson 4.4.0 and junit-jupiter. One thread acquires and releases the same lock in a loop while the test thread interrupts it. A short lockWatchdogTimeout makes a leak visible quickly. The race is timing-dependent; increase the duration or run a few threads if it does not reproduce on the first try.
import org.junit.jupiter.api.Test;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.assertTrue;
class WatchdogRenewalLeakTest {
@Test
void interruptedAcquireCanLeakTheWatchdog() throws Exception {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
config.setLockWatchdogTimeout(1000);
RedissonClient redisson = Redisson.create(config);
String name = "test:watchdog:leak";
redisson.getKeys().delete(name);
AtomicBoolean stop = new AtomicBoolean();
Thread worker = new Thread(() -> {
RLock lock = redisson.getLock(name);
while (!stop.get()) {
Thread.interrupted(); // clear so the next interrupt lands during the acquire
try {
lock.lockInterruptibly();
} catch (InterruptedException | RuntimeException e) {
// acquire was interrupted (Redisson may wrap it as RedisException)
} finally {
try {
lock.unlock(); // release whether or not we got it
} catch (RuntimeException ignored) {
// not held, or interrupted again during unlock
}
}
}
});
worker.start();
long until = System.currentTimeMillis() + 20_000;
while (System.currentTimeMillis() < until) {
worker.interrupt();
}
stop.set(true);
worker.interrupt();
worker.join();
Thread.sleep(3000); // longer than the watchdog TTL; a released key is gone by now
long ttl = redisson.getMap(name).remainTimeToLive();
redisson.getKeys().delete(name);
redisson.shutdown();
assertTrue(ttl <= 0,
"lock key survived with ttl=" + ttl + "ms after the worker stopped; the watchdog is renewing an orphan");
}
}Confirmed reproduced with Redisson 4.4.0 against a stock Redis started with docker run --rm -p 6379:6379 redis:8.2.2: after the worker stops and 3 s pass, the lock key is still present with its TTL refreshed to ~1000 ms (the configured lockWatchdogTimeout) even though no thread holds it — the watchdog is renewing an orphan.
Suggested fix
Order or reconcile renewLock and cancelLockRenewal so a cancel issued before its renewLock is not lost. For example, a cancel could leave a short-lived tombstone that suppresses a following renewLock for the same thread, or the acquire's thenApply could skip or undo renewal when the acquire has already been cancelled.
Activity
adityaanikam commentedon Jul 31, 2026
Verified this against current
masterthe code you cited matches exactly (RenewalTask.add/cancelExpirationRenewaloperate onname2entrywith no ordering between the two async callers). Excellent root-cause work, especially catching thatlockInterruptibly()'s early return on interrupt is what exposes the race.I'd like to work on this, along the lines of your suggested tombstone approach: add a short-lived
Set<String>(orMap<String, Long>keyed by rawName+threadId with a small TTL) inRenewalTaskthatcancelExpirationRenewalwrites to when it doesn't find an entry to cancel, and thatadd()checks before registering if a tombstone exists for that thread, consume it and skip arming renewal instead of registering. Keeps the fix local toRenewalTaskwithout touching the acquire/unlock call sites. Let me know if you'd rather see a different shape (e.g. reordering via the acquire future itself) before I put up a PR.ptanov commentedon Jul 31, 2026
Thanks for the quick turnaround.
The tombstone works, and it's the smaller change. If you go that way, two things I'd watch:
The tombstone write and the check have to be atomic with
name2entry, or the race just moves. If it's a separate set/map,add()can check for a tombstone beforecancelExpirationRenewalhas written one, and you're back toadd()registering while the cancel gets dropped. Putting both the check and the write inside a singlename2entry.compute(...)on that key serializes the two callers, so a cancel can't slip in between add's check and add's register.Also, a tombstone keyed on
rawName + threadIdcan eat a legit re-acquire from the same thread. Thread releases (cancel drops a tombstone), then grabs the same lock again while the tombstone is still around,add()consumes it and skips arming renewal, and now you've got a held lock with no watchdog. Virtual threads dodge it since the ids differ, but a pooled platform thread will reuse the id. So either tie the tombstone to the one in-flightaddwith a generation token, or keep its lifetime short enough that a real re-acquire can't hit it.You asked if I'd rather a different shape, so here's one I think is cleaner: schedule the renewer before the SET, not in the
thenApplyafter it. CallscheduleExpirationRenewal(threadId)beforetryLockInnerAsyncgoes out and the entry is inname2entrybefore the acquire can return or get interrupted. Then unlock's cancel can't run ahead of the register, so there's no window to lose it in. No TTL, no thread-id guessing.Downside is you're arming a watchdog before you know you got the lock, so the
thenApplyhas to de-register whenttlRemaining != null, and that runs on every failed attempt of a contendedlock(). And it leans on the renewer bailing out whenhexists(key, threadId)is false, for the case where the acquire never finishes and nothing cancels, so double-checkrenewExpirationactually drops the entry on a false result, otherwise you've traded a renewed-key leak for a dead-timer one.Given the choice I'd lean pre-schedule. The thing that sold me: the tombstone, in any form, has to remember that a cancel happened until the late schedule can notice it, and that memory is the awkward part - where it lives, when it expires, which acquire it matches. The TTL is a timing guess, and the generation token I mentioned for the ABA doesn't really escape it, it just moves the same "when do I forget this" bookkeeping somewhere else. Pre-scheduling deletes the out-of-order case instead of bookkeeping around it, so there's nothing to remember and nothing extra to clean up. It costs the speculative arm above, which is bounded and self-heals.
So it's the smaller diff versus the smaller surface. I'd take the surface, but either works and I'm happy to review whichever you put up.
test: strengthen watchdog cancel race coverage (redisson#7272)