Skip to content

Lock watchdog: renewal tasks multiply each time the set of renewed locks goes empty and non-empty again #7454

Done

Description

@garvit-joshi

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 20 second runs lower lockWatchdogTimeout to 3000 so that the effect shows quickly. The 4 minute run keeps the default 30000.

What is the Expected behavior?

One periodic renewal task per client for each kind of lock. While a watchdog lock is held, it sends one renewal script every lockWatchdogTimeout / 3, which is one every 10 seconds with the default setting.

What is the Actual behavior?

The client collects more and more renewal tasks that all keep running, and each of them sends the renewal script on its own. One more is added every time the client takes a watchdog lock while it holds no other one. A client that takes locks one after another (a worker loop, a scheduled job that locks per item) sends a growing stream of renewal scripts for a single lock.

Default settings

One thread, one lock: lock(), 100 ms of work, unlock(), 1 ms pause. Redisson 4.8.0, lockWatchdogTimeout 30000.

after lock cycles renewal scripts sent renewal scripts per second, over the last 10 s expected per second
30 s 281 279 18 0.1
60 s 563 1,339 44 0.1
120 s 1,129 5,376 83 0.1
180 s 1,694 11,559 114 0.1
240 s 2,258 19,558 144 0.1

One periodic task would have sent 24 renewal scripts in those 240 seconds. The rate was still rising when the run ended.

Every 4.x release tried, and master

The same loop with lockWatchdogTimeout 3000 and 20 ms of work, for 20 seconds. One periodic task sends at most 20 renewal scripts in that time.

Redisson lock cycles renewal scripts
4.0.0 744 3,128
4.3.0 756 3,407
4.5.0 745 2,462
4.6.1 756 3,414
4.7.0 738 2,857
4.8.0 735 2,763
master (b019f0e) 733 2,924
4.8.0 with the fix suggested below 716 18

Controls

Redisson 4.8.0, same settings, 20 seconds:

run renewal scripts
one lock() held for the whole 20 seconds 18
the same loop with a fixed lease, lock(10, TimeUnit.SECONDS) 0

A single hold is renewed at the designed rate. Only taking and releasing multiplies it.

It hides under concurrency

Redisson 4.8.0, the same loop and settings on N threads, one lock per thread, 20 seconds:

threads lock cycles renewal scripts
1 736 2,871
2 1,494 2,276
4 3,042 1,233
8 6,192 354
32 25,016 19

With many locks held at the same time, the set of renewed locks is almost never empty, so hardly any task is added. A load test with many concurrent locks does not show the problem. A quiet worker that holds one lock at a time shows it at full strength.

In a Redis outage every leftover task logs its own error

Redisson 4.8.0, lockWatchdogTimeout 3000. After the 20 second loop, the client took one other lock and held it, and Redis was frozen for 6 seconds (kill -STOP). timeout was 500 and retryAttempts 1, so that a renewal fails inside the freeze.

before the freeze Can't update locks [probe:outage:held] expiration ERROR lines
the 20 second loop (721 lock cycles) 243
nothing 1

The lock named in those lines is not the one that was cycled. Each leftover task renews every lock the client holds at that moment.

Additional information

Cause

RenewalTask.stop() clears running, but the timeout that is already scheduled stays in the timer. add() schedules another one whenever tryRun() succeeds, which is every time a lock is added after the map became empty.

RenewalTask.java#L54-L69 (the file is identical in 4.8.0):

boolean tryRun() {
    return running.compareAndSet(false, true);
}

void stop() {
    running.set(false);
}

public void schedule() {
    if (!running.get()) {
        return;
    }

    long internalLockLeaseTime = executor.getServiceManager().getCfg().getLockWatchdogTimeout();
    executor.getServiceManager().newTimeout(this, internalLockLeaseTime / 3, TimeUnit.MILLISECONDS);
}

Step by step:

  1. Lock A is taken. add() finds no entry, tryRun() sets running to true, and schedule() arms timeout T1.
  2. Lock A is released. cancelExpirationRenewal() removes the entry, the map is empty, and stop() sets running to false. T1 is still armed.
  3. A lock is taken again, A or any other. add() finds no entry, tryRun() succeeds again, and schedule() arms timeout T2.
  4. T1 fires. run() cannot tell that it was armed before the stop(). It renews, then calls schedule(), which sees running true and arms T1 again. T2 does the same. From here on both run every lockWatchdogTimeout / 3.

Every time the map goes from empty to non-empty, one more periodic task is added. A task ends only if its schedule() call finds running false, that is, if it fires at a moment when no lock is held. In the default-settings run above the lock is held more than 90% of the time, so nearly all of them survive.

stop(), schedule() and run() are in the base class, so LockTask, ReadLockTask and FastMultilockTask all inherit this. Only RLock was measured.

This is not #7272. That one is about a renewal that keeps running after unlock(). Here every lock is released and renewed correctly, and only the number of timer tasks is wrong.

Suggested fix

Allow at most one pending timeout:

@@ RenewalTask, fields
     AtomicBoolean running = new AtomicBoolean();
+    private final AtomicBoolean scheduled = new AtomicBoolean();
@@ schedule()
     public void schedule() {
-        if (!running.get()) {
+        if (!running.get() || !scheduled.compareAndSet(false, true)) {
             return;
         }
@@ run()
     @Override
     public void run(Timeout timeout) {
+        scheduled.set(false);
         if (executor.getServiceManager().isShuttingDown()) {

Measured on 4.8.0 with only this class replaced: 716 lock cycles and 18 renewal scripts in 20 seconds, where the unpatched 4.8.0 sent 2,763.

A lock that is added while a timeout is pending gets its first renewal when that timeout fires. That is at most lockWatchdogTimeout / 3 after the lock was taken, the same as today for a lock that is added while the task is already running.

A pull request with this change and a test can follow.

Reproducer

RenewalTimers.java
import java.util.Map;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.api.redisnode.RedisNode;
import org.redisson.api.redisnode.RedisNodes;
import org.redisson.config.Config;

/**
 * One thread takes and releases one watchdog lock in a loop. Every 5 s it prints how many scripts
 * Redis has run beyond the two (lock, unlock) that each cycle needs: those are renewal scripts.
 *
 * args: [lockWatchdogTimeout ms, default 3000] [ms the lock is held per cycle, default 20] [seconds, default 20]
 */
public class RenewalTimers {

    public static void main(String[] args) throws Exception {
        long watchdog = args.length > 0 ? Long.parseLong(args[0]) : 3000;
        long hold = args.length > 1 ? Long.parseLong(args[1]) : 20;
        int seconds = args.length > 2 ? Integer.parseInt(args[2]) : 20;

        Config config = new Config();
        config.useSingleServer().setAddress(System.getProperty("redis", "redis://127.0.0.1:6379"));
        config.setLockWatchdogTimeout(watchdog);
        RedissonClient redisson = Redisson.create(config);
        RLock lock = redisson.getLock("renewal-timers");

        long scriptsAtStart = scripts(redisson);
        long start = System.currentTimeMillis();
        long cycles = 0;
        long lastRenewals = 0;
        double lastPrint = 0;
        System.out.println("  t(s)  lock cycles  renewal scripts  renewal scripts/s");
        while (true) {
            double t = (System.currentTimeMillis() - start) / 1000.0;
            if (t - lastPrint >= 5) {
                long renewals = scripts(redisson) - scriptsAtStart - 2 * cycles;
                System.out.printf("%6.1f %12d %16d %18.1f%n", t, cycles, renewals, (renewals - lastRenewals) / (t - lastPrint));
                lastRenewals = renewals;
                lastPrint = t;
                if (t >= seconds) {
                    break;
                }
            }
            lock.lock();
            try {
                Thread.sleep(hold);
            } finally {
                lock.unlock();
            }
            cycles++;
            Thread.sleep(1);
        }
        System.out.println("one periodic renewal task sends at most " + seconds * 3000L / watchdog + " renewal scripts in " + seconds + " s");
        redisson.shutdown();
    }

    /** EVAL and EVALSHA calls the server has run so far. */
    static long scripts(RedissonClient redisson) {
        Map<String, String> stats = redisson.getRedisNodes(RedisNodes.SINGLE).getInstance()
                .info(RedisNode.InfoSection.COMMANDSTATS);
        long calls = 0;
        for (String command : new String[] {"cmdstat_eval", "cmdstat_evalsha"}) {
            String value = stats.get(command); // calls=123,usec=456,...
            if (value != null) {
                calls += Long.parseLong(value.substring("calls=".length(), value.indexOf(',')));
            }
        }
        return calls;
    }
}

Run it against a Redis that nothing else is using, because it counts every script call on the server:

java RenewalTimers                 # lockWatchdogTimeout 3000, 20 ms of work, 20 seconds
java RenewalTimers 30000 100 240   # default watchdog, 100 ms of work, 4 minutes
java RenewalTimers 3000 20000 20   # the single-hold control
java -Dredis=redis://host:port RenewalTimers

The fixed-lease, thread and outage runs used small variants of this program. They can be posted if they are useful.

How renewal scripts are counted: INFO commandstats gives the number of EVAL and EVALSHA calls. One lock cycle runs exactly two scripts, one for lock() and one for unlock(). Everything above that is a renewal. A MONITOR capture of a 5 second run confirmed it: 179 lock cycles, 180 calls of the lock script, 180 of the unlock script and 241 of the renewal script, each including one NOSCRIPT first call on a fresh server. The runs in the tables started with all three scripts already loaded.

Raw output

20 second runs, per version
===== Redisson 4.0.0 =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          181              294               58.8
  10.0          368             1102              161.4
  15.0          558             2132              205.5
  20.0          744             3128              198.8
one periodic renewal task sends at most 20 renewal scripts in 20 s

===== Redisson 4.3.0 =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          185              282               56.2
  10.0          377             1137              170.9
  15.0          566             2239              220.3
  20.0          756             3407              233.0
one periodic renewal task sends at most 20 renewal scripts in 20 s

===== Redisson 4.5.0 =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          181              299               59.6
  10.0          366              960              131.7
  15.1          556             1742              155.8
  20.1          745             2462              143.4
one periodic renewal task sends at most 20 renewal scripts in 20 s

===== Redisson 4.6.1 =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          185              300               59.9
  10.0          370             1120              164.0
  15.0          563             2176              210.2
  20.1          756             3414              246.6
one periodic renewal task sends at most 20 renewal scripts in 20 s

===== Redisson 4.7.0 =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          178              265               52.9
  10.0          363              904              127.5
  15.0          551             1796              177.9
  20.1          738             2857              211.4
one periodic renewal task sends at most 20 renewal scripts in 20 s

===== Redisson 4.8.0 =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          177              237               47.2
  10.0          360              979              147.9
  15.1          547             1844              172.4
  20.1          735             2763              182.9
one periodic renewal task sends at most 20 renewal scripts in 20 s

===== Redisson master b019f0e =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          174              268               53.6
  10.0          356              997              145.5
  15.0          544             1904              181.0
  20.1          733             2924              203.0
one periodic renewal task sends at most 20 renewal scripts in 20 s

===== Redisson 4.8.0 with the patched RenewalTask =====
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0          174                4                0.8
  10.0          355                9                1.0
  15.0          539               14                1.0
  20.0          716               18                0.8
one periodic renewal task sends at most 20 renewal scripts in 20 s
4 minute run with default settings, Redisson 4.8.0
  t(s)  lock cycles  renewal scripts  renewal scripts/s
   5.0           46                0                0.0
  10.1           93                1                0.2
  15.2          140               48                9.3
  20.3          187               95                9.3
  25.3          234              185               17.9
  30.3          281              279               18.6
  35.3          328              412               26.6
  40.4          375              545               26.5
  45.4          422              724               35.4
  50.4          469              898               34.7
  55.5          516             1121               44.3
  60.5          563             1339               43.3
  65.5          610             1602               52.4
  70.5          657             1856               50.5
  75.6          705             2164               60.4
  80.7          752             2452               57.3
  85.7          799             2781               65.2
  90.7          846             3098               63.3
  95.7          893             3440               68.3
 100.7          940             3788               69.4
 105.8          988             4166               74.4
 110.8         1035             4548               76.4
 115.8         1082             4955               81.2
 120.8         1129             5376               83.9
 125.9         1176             5811               86.4
 130.9         1223             6272               92.2
 135.9         1270             6727               90.8
 140.9         1317             7219               98.0
 145.9         1364             7701               96.1
 150.9         1411             8218              103.0
 156.0         1458             8726              100.7
 161.0         1505             9274              109.2
 166.0         1552             9803              105.7
 171.1         1600            10404              117.8
 176.2         1647            10947              107.8
 181.2         1694            11559              122.3
 186.2         1741            12116              110.9
 191.2         1788            12757              127.8
 196.2         1835            13352              118.8
 201.2         1882            14011              131.5
 206.2         1929            14636              124.7
 211.3         1976            15339              139.6
 216.3         2023            15987              128.6
 221.3         2070            16709              143.7
 226.3         2117            17392              136.4
 231.4         2164            18117              144.6
 236.4         2211            18830              141.8
 241.4         2258            19558              145.0
one periodic renewal task sends at most 24 renewal scripts in 240 s

Test machine: macOS 27.2.1 on arm64, OpenJDK 25.0.5.1 (Temurin), Redis 8.12.0 on localhost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions