Repository navigation
Make partitions and readFrom volatile in PooledClusterConnectionProvider #3777 - #3783
Merged
Merged
Conversation
…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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 5aded25. Configure here.
2 tasks done
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Overview
partitionsandreadFromvolatile inPooledClusterConnectionProviderstateLock; reads now have the previously-missing happens-before guaranteeProblem
partitionsandreadFromare mutated understateLock(setPartitions,setReadFrom), andsetPartitionseven documents the intent: "Synchronize onstateLockto initiate a happens-before relation". However, every read path reads the fields without acquiring the lock —getConnectionAsync,getWriteConnection,getReadConnection,getRandomConnectionAsync,isStale,getPartitions,getReadFrom, andNodeConnectionPostProcessor.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 stalePartitions/ReadFromafter a topology update for an unbounded time.Notes
PooledClusterConnectionProviderUnitTestsstill 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.
partitionsandreadFromare still updated understateLockinsetPartitionsandsetReadFrom, 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.