[Fix] ConcurrentLongHashMap throw ArrayIndexOutOfBoundsException#4771
Open
void-ptr974 wants to merge 7 commits intoapache:masterfrom
Open
[Fix] ConcurrentLongHashMap throw ArrayIndexOutOfBoundsException#4771void-ptr974 wants to merge 7 commits intoapache:masterfrom
void-ptr974 wants to merge 7 commits intoapache:masterfrom
Conversation
When concurrent read write access the map, The key array and value array are not publish at the same time when shrink or expand. This fix encapsulate the key and value in the same field to avoid this happen
When concurrent read write access the map, The key array and value array are not publish at the same time when shrink or expand. This fix encapsulate the key and value in the same field to avoid this happen
When concurrent read write access the map, The key array and value array are not publish at the same time when shrink or expand. This fix encapsulate the key and value in the same field to avoid this happen
When concurrent read write access the map, The key array and value array are not publish at the same time when shrink or expand. This fix encapsulate the key and value in the same field to avoid this happen
When concurrent read write access the map, The key array and value array are not publish at the same time when shrink or expand. This fix encapsulate the key and value in the same field to avoid this happen
merlimat
approved these changes
May 2, 2026
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.
Fix #4684
Bug
ConcurrentLongHashMap.Sectionpublisheskeys,values,capacityas three separatevolatilefields. Duringrehasha reader can observe a torn snapshot — e.g. newkeys(length 8)with old
values(length 4) — then computebucket = signSafeMod(hash, values.length)∈ [0,8) and indexkeys[bucket]against length 4, throwingArrayIndexOutOfBoundsExceptionatSection.get. The exception fires before the optimistic-readvalidate(stamp)check, so the existing fallback can't recover.Reproduce in ≈ 2 minutes
ArrayIndexOutOfBoundsExceptionfires within the first warmup iteration. 10/10 reproductions across 10 fresh JVMs on the unpatched code, 0/10 after this PR.Fix
Bundle
keys,values,capacityinto one immutable inner class published via a single volatile reference:Final-field semantics plus one volatile-acquire-release pair give the reader a consistent triple from one read. The partial-publish window is impossible by construction.
Tests added
bookkeeper-server/src/test/.../ConcurrentLongHashMapTest:testNoLostGetAfterPublish— writer putskthen publisheskvia a volatile counter; every reader observing the counter must see a non-null value fork.testCorrectnessAgainstConcurrentHashMap— 8 threads × 50K random ops mirrored toConcurrentHashMap; per-call return values and final state must match.testConcurrentMultiWriterExpandShrink— 8 writers × 8 readers on a single section withautoShrink; no torn(k,v)pair is allowed to surface.testForEachDuringWrites—forEachduring concurrent puts/removes must not throw or expose internal sentinels.testConcurrentExpandAndShrinkAndGet— strengthened existing test.mvn -pl bookkeeper-server -Dtest=ConcurrentLongHashMapTest test23/23 pass in 4s.
A JMH benchmark (
ConcurrentLongHashMapBenchmarkinmicrobenchmarks) is included so this race can be regression-tested on every JDK / hardware change.Master Issue: #4684