fix: use ConcurrentHashMap for Version.VERSION2INT to prevent concurrent corruption#16171
Open
daguimu wants to merge 1 commit intoapache:3.3from
Open
fix: use ConcurrentHashMap for Version.VERSION2INT to prevent concurrent corruption#16171daguimu wants to merge 1 commit intoapache:3.3from
daguimu wants to merge 1 commit intoapache:3.3from
Conversation
…ent corruption VERSION2INT uses a plain HashMap that is read and written concurrently from the RPC hot path (isSupportResponseAttachment is checked on every call). Concurrent put() during HashMap resize can corrupt the internal hash table, causing threads to hang in infinite loops.
178e685 to
f992a6a
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 3.3 #16171 +/- ##
============================================
- Coverage 60.81% 60.80% -0.02%
+ Complexity 11765 11753 -12
============================================
Files 1953 1953
Lines 89118 89118
Branches 13444 13444
============================================
- Hits 54197 54188 -9
- Misses 29364 29369 +5
- Partials 5557 5561 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Problem
Version.VERSION2INTuses a plainHashMapthat is accessed concurrently from the RPC hot path:This is called via
isSupportResponseAttachment()on every RPC call to check protocol compatibility.Impact
Concurrent
put()duringHashMapinternal resize can corrupt the hash table's bucket chain, causing threads to hang permanently in an infinite loop duringget()— a well-documented JDK issue with unsynchronizedHashMapaccess. This is a classic production incident pattern that is difficult to diagnose.Root Cause
HashMapwas used whereConcurrentHashMapis required. The code comment on the field even notes it is used for performance ("int compare expect to has higher performance than string"), but the collection itself is not thread-safe.Fix
Replace
new HashMap<>()withnew ConcurrentHashMap<>(). The check-then-act pattern ingetIntVersion()is acceptable because:ConcurrentHashMapTests Added
testGetIntVersionConcurrency— 10 threads × 1000 iterations callinggetIntVersionwith various version strings concurrently, verifying consistent resultsAll 9 tests in
VersionTestpass.Impact
HashMap→ConcurrentHashMap