Skip to content

Make partitions and readFrom volatile in PooledClusterConnectionProvider #3777 - #3783

Merged
a-TODO-rov merged 2 commits into
redis:mainfrom
big-cir:issue/3777
Aug 26, 2026
Merged

a-TODO-rov merged 2 commits into
redis:mainfrom
big-cir:issue/3777

Conversation

@big-cir

@big-cir big-cir commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Overview

  • marked partitions and readFrom volatile in PooledClusterConnectionProvider
  • writes keep using stateLock; reads now have the previously-missing happens-before guarantee

Problem

partitions and readFrom are mutated under stateLock (setPartitions, setReadFrom), and setPartitions even documents the intent: "Synchronize on stateLock to initiate a happens-before relation". However, every read path reads the fields without acquiring the lock — getConnectionAsync, getWriteConnection, getReadConnection, getRandomConnectionAsync, isStale, getPartitions, getReadFrom, and NodeConnectionPostProcessor.apply. Under the JMM a lock only establishes happens-before between an unlock and a subsequent lock of the same lock, so lock-guarded writes plus unsynchronized reads establish no visibility guarantee: reader threads may observe a stale Partitions/ReadFrom after a topology update for an unbounded time.

Notes

  • Memory-visibility fix; the JMM violation is verifiable by inspection but not by a deterministic test (a stale read is timing/JIT dependent). Existing PooledClusterConnectionProviderUnitTests still pass (18/18), confirming no regression.

Related


Note

Low Risk
Narrow concurrency visibility fix with no API or logic changes; behavior is unchanged aside from making cluster topology and ReadFrom updates visible to other threads promptly.

Overview
Fixes a Java Memory Model visibility gap in cluster connection routing after topology or read-preference updates.

partitions and readFrom are still updated under stateLock in setPartitions and setReadFrom, but many hot paths read them without holding that lock (slot connections, random node selection, stale-connection checks, etc.). A lock only orders visibility for threads that later acquire the same lock, so those unsynchronized reads could keep seeing an old cluster view indefinitely. Marking both fields volatile gives readers a proper happens-before edge from the publishing writes without changing locking or call-site behavior.

Reviewed by Cursor Bugbot for commit 23f43c6. Bugbot is set up for automated code reviews on this repo. Configure here.

…der redis#3777

The partitions and readFrom fields are mutated under stateLock but were
read without synchronization, so topology updates had no happens-before
guarantee for reader threads. Marking both fields volatile establishes
the missing read visibility while writes keep using stateLock.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 5aded25. Configure here.

@a-TODO-rov
a-TODO-rov merged commit d5cf4b1 into redis:main Aug 26, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PooledClusterConnectionProvider reads partitions/readFrom without synchronization

2 participants