Skip to content

Conversation

@kilink
Copy link
Contributor

@kilink kilink commented Nov 13, 2025

The Spliterators returned by values, entrySet, and keySet incorrectly reported the SIZED characteristic, instead of CONCURRENT. This could lead to bugs when the map is concurrently modified during a stream operation.

For keySet and values, the incorrect characteristics are inherited from AbstractMap, so to rectify that the respective methods are overridden and custom collections are provided that report the correct Spliterator characteristics.

The same sort of bug was reported and fixed in Caffeine, and the issue seems to exist in several other ConcurrentMap implementations in the wild.

The Spliterators returned by values, entrySet, and keySet incorrectly reported
the SIZED characteristic, instead of CONCURRENT. This could lead to bugs when
the map is concurrently modified during a stream operation.

For keySet and values, the incorrect characteristics are inherited from
AbstractMap, so to rectify that the respective methods are overridden and
custom collections are provided that report the correct Spliterator
characteristics.

Signed-off-by: Patrick Strawderman <[email protected]>
@kilink kilink force-pushed the concurrent-reference-hash-map-fix-spliterators branch from aefaa72 to e4b7baf Compare November 13, 2025 20:49
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 13, 2025
* Late binding entry set.
*/
private volatile @Nullable Set<Map.Entry<K, V>> entrySet;
private @Nullable Set<Map.Entry<K, V>> entrySet;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I remove volatile here, and didn't make the new fields volatile, since this is just a benign race; ConcurrentHashMap does the same thing. Seems preferable to paying the cost of the volatile field access.

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

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants