Skip to content

SharedLock.lockWritersExclusive still spins RUNNABLE at 100% CPU on 6.8.2 when another thread's shared writers never drain (residual of #2905 / #2961) #3880

Open

Description

@mdsarfarazulh

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= climbing 1.02M ms → 2.51M ms → 4.30M ms (~one core pegged the whole window).
  • Zero SharedLock.incrementWriters waiters — 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's ReentrantLock at SharedLock.doExclusive inside CommandHandler.channelInactive
    → so the channel never goes inactive and ConnectionWatchdog never 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

Possible Solution

  1. (Preferred) Replace the WRITERS CAS busy-loop + inner ReentrantLock with a
    synchronized/AQS Condition: the exclusive waiter parks and is signalled when writers reach
    the exclusive-eligible state. Removes the CPU burn and makes the wait interruptible.
  2. Add tryLockWritersExclusive(timeout) so doExclusive fails fast (failed future / exception)
    instead of spinning indefinitely — converts a permanent wedge into a recoverable, observable error.
  3. 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.
  4. Guard every incrementWriters with a matching decrementWriters in finally; warn/assert when
    WRITERS fails 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

  1. a-TODO-rov commented on Aug 5, 2026

    @a-TODO-rov
    Collaborator

    Hi @mdsarfarazulh,

    Same family as #3804, and the same points apply:

    • Corner case — needs an Error (SOE) that skips decrementWriters(); ordinary exceptions and contention can't cause it.
    • Don't swap the primitive — an AQS Condition rewrite has the same risk as the synchronized revert: reintroducing Revisit synchronized blocks #1429 carrier-pinning on the hottest lock we have. Off the table.
    • Fix doExclusive — bound the acquire and the writers-spin (Thread.onSpinWait()), fail fast with RedisException. Your options 2 and 3.

    Let's track both as one fix, do you agree ?

  2. mdsarfarazulh commented on Aug 5, 2026

    @mdsarfarazulh
    Author

    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?

  3. a-TODO-rov commented on Aug 7, 2026

    @a-TODO-rov
    Collaborator

    @mdsarfarazulh probably makes sense. Lets first see the solution itself - we can add client config later.

  4. nvs2022 commented on Sep 25, 2026

    @nvs2022

    When will this be fixed?

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions