Repository navigation
Description
SharedLock.lockWritersExclusive still spins RUNNABLE at 100% CPU on 6.8.2 when another thread's shared writers never drain (residual of #2905 / #2961)
Current Behavior
#2905 / PR #2961 (shipped in 6.5.0) fixed the same-thread reentrant self-deadlock by
changing the exclusive-acquire CAS from (WRITERS == 0) to (WRITERS == this thread's count).
That closes the case where the spinning thread is itself the writer.
It does not close the case where the outstanding shared writers belong to a different
thread (or were leaked and never decremented). On 6.8.2, lockWritersExclusive is:
while (!WRITERS.compareAndSet(this, threadWriters(), -1)) {
// busy-loop: succeeds only when GLOBAL WRITERS == this thread's own writer count.
// If any OTHER thread holds/leaked a shared writer, global WRITERS stays higher forever
// -> CAS never succeeds -> RUNNABLE spin, no timeout, no bound, no yield.
}So a closeAsync() / doExclusive() on an endpoint whose shared-writer count can never drain
to the caller's own count spins RUNNABLE forever, pegging one CPU core. Only a JVM kill recovers.
Observed (Lettuce 6.8.2, OpenJDK 21)
Single pod, single ClusterNodeEndpoint, 12 thread dumps over ~57 min:
- One request thread RUNNABLE in
SharedLock.lockWritersExclusive— the same thread in every dump. cpu=climbing1.02M ms → 2.51M ms → 4.30M ms(~one core pegged the whole window).- Zero
SharedLock.incrementWriterswaiters — this is not shared-path contention; it's a pure exclusive-acquire spin. - In the final dump a
lettuce-epollEventLoop-*IO thread is parked behind the spinner on the
same endpoint'sReentrantLockatSharedLock.doExclusiveinsideCommandHandler.channelInactive
→ so the channel never goes inactive andConnectionWatchdognever reconnects. Endpoint is permanently wedged.
Spinner stack:
io.lettuce.core.protocol.SharedLock.lockWritersExclusive (SharedLock.java:129) <-- RUNNABLE spin
io.lettuce.core.protocol.SharedLock.doExclusive (SharedLock.java:103)
io.lettuce.core.protocol.DefaultEndpoint.doExclusive (DefaultEndpoint.java:748)
io.lettuce.core.cluster.ClusterNodeEndpoint.closeAsync (ClusterNodeEndpoint.java:55)
... CommandExpiryWriter / CommandListenerWriter / RedisChannelHandler.closeAsync ...
<application-initiated closeAsync()>
This differs from the classic #2905 report (which reached lockWritersExclusive via
notifyChannelInactive on 6.3.x, same-thread reentrant) — here it is reached via the
closeAsync() path on 6.8.2, and the stuck writers are not the spinner's own.
Input Code
Repro sketch: on one endpoint, hold/leak a shared writer on thread A (e.g. throw between
incrementWriters and decrementWriters), then call closeAsync() on thread B → thread B's
lockWritersExclusive CAS can never match (global WRITERS > B's own count) → spins forever.
Expected behavior/code
closeAsync() / doExclusive() should complete or fail fast. The exclusive acquire should be
bounded, interruptible, or park-and-signal — never an unbounded busy-loop that pegs a core when
writers held by another thread fail to drain.
Environment
- Lettuce version: 6.8.2 (includes fix:deadlock when reentrant exclusive lock #2905 #2879 #2961; loop unchanged on
main) - Redis: Redis Cluster
- Java: OpenJDK 21
Possible Solution
- (Preferred) Replace the
WRITERSCAS busy-loop + innerReentrantLockwith a
synchronized/AQSCondition: the exclusive waiter parks and is signalled when writers reach
the exclusive-eligible state. Removes the CPU burn and makes the wait interruptible. - Add
tryLockWritersExclusive(timeout)sodoExclusivefails fast (failed future / exception)
instead of spinning indefinitely — converts a permanent wedge into a recoverable, observable error. - Minimum mitigation if the CAS stays:
Thread.onSpinWait()+ bounded spin count before escalating
to a parking wait, so a stuck acquire doesn't peg a core. - Guard every
incrementWriterswith a matchingdecrementWritersinfinally; warn/assert when
WRITERSfails to drain within a window, to surface the leaked-writer case fix:deadlock when reentrant exclusive lock #2905 #2879 #2961 doesn't cover.
Additional context
Same failure family as #2905 / #2961 (both spin in lockWritersExclusive), but #2961 only
handles same-thread reentrancy; the other-thread / leaked-writer case remains unbounded on 6.8.2.
Single-endpoint, single-JVM blast radius; other endpoints and clients in the same JVM stay healthy.
We can share sanitized dumps + cpu= progressions, and plan to capture jstack -l + a heap dump on
next recurrence to read the WRITERS field and the exclusive-lock owner — confirming whether the
spinner leaked its own writer vs. waiting on another thread's.
Activity
a-TODO-rov commentedon Aug 5, 2026
Hi @mdsarfarazulh,
Same family as #3804, and the same points apply:
Let's track both as one fix, do you agree ?
mdsarfarazulh commentedon Aug 5, 2026
Hi @a-TODO-rov,
Thanks for your reply. For timeout in option 2 I am thinking for adding it to ClientOptions so that users can configure it as per their convenience with a default fallback of 30 seconds. Does this looks good to you?
a-TODO-rov commentedon Aug 7, 2026
@mdsarfarazulh probably makes sense. Lets first see the solution itself - we can add client config later.
Bound exclusive lock acquisition in SharedLock to fail fast on a dead…
Bound exclusive lock acquisition in SharedLock to fail fast on a dead…
Bound exclusive lock acquisition in SharedLock to fail fast on a dead…
nvs2022 commentedon Sep 25, 2026
When will this be fixed?
fix(protocol): ensure Java 8 compatibility and protect endpoint lifec…
fix(lifecycle): guard notifyChannelActive, flushCommands, and cancelB…
fix(lifecycle): use finally blocks to ensure channel deactivation and…
fix(protocol): enforce single timeout budget and guarantee endpoint c…