[ISSUE #10214] Fix TOCTOU race condition in MQClientInstance.brokerVersionTable#10215
Open
daguimu wants to merge 1 commit intoapache:developfrom
Open
[ISSUE #10214] Fix TOCTOU race condition in MQClientInstance.brokerVersionTable#10215daguimu wants to merge 1 commit intoapache:developfrom
daguimu wants to merge 1 commit intoapache:developfrom
Conversation
…okerVersionTable Fixes apache#10214
8bbff9f to
7341577
Compare
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
MQClientInstance.brokerVersionTableis accessed using non-atomic check-then-act patterns in three locations. BetweencontainsKey()andget(), another thread could remove the entry, causing a NullPointerException.Root Cause
The
sendHeartbeatToBroker(),sendHeartbeatToAllBrokerV2(), andfindBrokerVersion()methods use separatecontainsKey/put/getcalls on aConcurrentHashMap, which are individually thread-safe but not atomic as a compound operation. A concurrent removal between these calls leads to NPE.Notably, a similar operation in
HeartbeatTask.execute()(line ~814) already uses the correctcomputeIfAbsentpattern.Fix
sendHeartbeatToBroker(): ReplacecontainsKey/put/getwithcomputeIfAbsentfor atomic put-if-absent and value retrieval in one step.sendHeartbeatToAllBrokerV2(): Same fix as above.findBrokerVersion(): Cache the result ofget()in a local variable before dereferencing, eliminating the TOCTOU window.Tests Added
findBrokerVersionwith missing broker nametestFindBrokerVersionWhenBrokerNameNotExist()— verifies no NPE when broker name is absentfindBrokerVersionwith missing broker addrtestFindBrokerVersionWhenBrokerAddrNotExist()— verifies no NPE when addr is absentfindBrokerVersionafter concurrent removaltestFindBrokerVersionConcurrentRemoval()— simulates removal between check and getcomputeIfAbsentatomicity for heartbeat pathstestBrokerVersionTableComputeIfAbsent()— verifies atomic creation and idempotencyImpact
Only affects
brokerVersionTableaccess inMQClientInstance. No behavioral change for normal operation — only eliminates potential NPE under concurrent broker registration/removal.Fixes #10214