Senrian opened a new pull request, #10235:
URL: https://github.com/apache/rocketmq/pull/10235

   ## Bug Description
   
   Issue: #10214
   
   In `MQClientInstance.java`, `brokerVersionTable` (a `ConcurrentHashMap`) is 
accessed using a non-atomic check-then-act pattern that can cause 
NullPointerException under concurrent access.
   
   ### Locations Fixed
   
   1. **sendHeartbeatToBroker()** - Race between containsKey() and get()
   2. **sendHeartbeatToBrokerV2()** - Same issue
   3. **findBrokerVersion()** - Triple get() calls with potential NPE
   
   ### Fix Pattern
   
   Use `computeIfAbsent` for atomic get-or-create:
   ```java
   // Before (race condition):
   if (!this.brokerVersionTable.containsKey(brokerName)) {
       this.brokerVersionTable.put(brokerName, new ConcurrentHashMap<>(4));
   }
   this.brokerVersionTable.get(brokerName).put(addr, version);
   
   // After (thread-safe):
   this.brokerVersionTable.computeIfAbsent(brokerName, k -> new 
ConcurrentHashMap<>(4))
           .put(addr, version);
   ```
   
   For findBrokerVersion(), use a local variable to avoid NPE if entry is 
removed between calls.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to