Skip to content

fix: prevent lock watchdog from outliving unlock (#7272) - #7277

Open
arimu1 wants to merge 2 commits into
redisson:masterfrom
arimu1:fix/7272-watchdog-renewal-cancel-race
Open

arimu1 wants to merge 2 commits into
redisson:masterfrom
arimu1:fix/7272-watchdog-renewal-cancel-race

Conversation

@arimu1

@arimu1 arimu1 commented Aug 1, 2026

Copy link
Copy Markdown

What problem does this PR solve?

Fixes #7272 — lock watchdog renewal can outlive unlock() when cancelLockRenewal races the acquire path's async scheduleExpirationRenewal.

After an interrupted lockInterruptibly() (or any case where unlock runs before the acquire's post-SET schedule callback), cancel can find no entry and no-op, then the late scheduleExpirationRenewal arms renewal forever. The key's TTL keeps jumping back to lockWatchdogTimeout with no holder.

How it works

  1. Pre-register the watchdog before the acquire SET (watchdog / leaseTime <= 0 path only), so unlock's cancel always sees the entry.
  2. Cancel renewal synchronously at unlock start (before the Redis unlock), so a following acquire cannot re-arm while cancel is still waiting on Redis.
  3. On failed acquire, undo the speculative registration.
  4. On cancelled/interrupted acquire, if the caller already cancelled the future after SET succeeded, drop the watchdog and unlock (same pattern as existing async lock paths).

Tests

  • LockEntryTest — counter semantics for speculative register / cancel / reentrant release (no Redis).
  • RedissonLockExpirationRenewalTest#testWatchdogDoesNotOutliveInterruptedAcquire — interrupt + unlock loop must not leave a renewed orphan (Docker Redis).
  • Existing testLockIsNotRenewedAfterInterruptedTryLock, reentrant renew, and basic lock tests pass.

Checklist

  • DCO signed (Signed-off-by: arimu1 <[email protected]>)
  • One issue per PR
  • No unrelated changes

Register expiration renewal before the acquire SET so cancel cannot
run before schedule. Cancel renewal synchronously at unlock start so
a following acquire cannot re-arm while cancel is still in-flight.
On interrupted/cancelled acquire, drop the speculative watchdog and
unlock if the SET already succeeded.

Signed-off-by: arimu1 <[email protected]>
* and release runs in finally (cancel vs schedule race). See #7272.
*/
@Test
public void testWatchdogDoesNotOutliveInterruptedAcquire() throws Exception {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test pass even without changes applied

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrniko Good catch — the previous interrupt-loop assertion was too weak and could pass without the production change.

Strengthened coverage in RedissonLockExpirationRenewalTest:

  1. testCancelledTryLockAsyncDoesNotLeaveWatchdog — cancels in-flight tryLockAsync futures and asserts the key is not left held/renewed. Intentionally does not call forceUnlock() after a successful cancel (that would mask the leak). Verified locally:
    • fails on master production (stillLocked stays true after cancels)
    • passes with this PR’s cancel handler + pre-schedule path
  2. testWatchdogDoesNotOutliveInterruptedAcquire — multi-worker continuous interrupt storm closer to the Watchdog renewal can outlive unlock(): cancelLockRenewal races the acquire's async scheduleExpirationRenewal #7272 reproducer (secondary stress).

Happy to adjust further if you want a different shape.

Replace the flaky interrupt-only assertion with a tryLockAsync cancel
path that fails without the production cancel handler (orphan key still
held/renewed) and passes with it. Amplify the interrupt storm as a
secondary stress case matching the issue reproducer.

Signed-off-by: arimu1 <[email protected]>
@arimu1

arimu1 commented Aug 3, 2026

Copy link
Copy Markdown
Author

@mrniko Addressed the review note on the test passing without the production change.

Added testCancelledTryLockAsyncDoesNotLeaveWatchdog, which fails without the cancel-handler fix (orphan lock remains held/renewed after cancelling in-flight tryLockAsync) and passes with it. Local check used Docker Redis via Testcontainers (JDK 25).

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Watchdog renewal can outlive unlock(): cancelLockRenewal races the acquire's async scheduleExpirationRenewal

2 participants