[fix][broker] ConcurrentLongHashMap throw ArrayIndexOutOfBoundsException#25644
Open
void-ptr974 wants to merge 3 commits intoapache:masterfrom
Open
[fix][broker] ConcurrentLongHashMap throw ArrayIndexOutOfBoundsException#25644void-ptr974 wants to merge 3 commits intoapache:masterfrom
void-ptr974 wants to merge 3 commits intoapache:masterfrom
Conversation
Bundle keys / values / capacity into one immutable inner class and publish via a single volatile reference
Bundle keys / values / capacity into one immutable inner class and publish via a single volatile reference
merlimat
reviewed
May 1, 2026
Contributor
merlimat
left a comment
There was a problem hiding this comment.
Great find!
I just left a couple of comments
Bundle keys / values / capacity into one immutable inner class and publish via a single volatile reference
Contributor
Author
|
Thanks @merlimat — both addressed in the latest commit. PTAL. |
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.
Fixes #25643
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
pulsar-common/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.20/20 pass in 2.3s.
A JMH benchmark (
ConcurrentLongHashMapBenchmarkin themicrobenchmodule) is included so this race can be regression-tested on every JDK / hardware change.Does this pull request potentially affect one of the following parts: