You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by "Senrian (via GitHub)" <gi...@apache.org> on 2026/03/30 09:22:03 UTC

[PR] [Bug] Fix TOCTOU race condition in MQClientInstance.brokerVersionTable access [rocketmq]

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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Bug] Fix TOCTOU race condition in MQClientInstance.brokerVersionTable access [rocketmq]

Posted by "Senrian (via GitHub)" <gi...@apache.org>.
Senrian commented on PR #10235:
URL: https://github.com/apache/rocketmq/pull/10235#issuecomment-4190592043

   Hi @Kris20030907, thank you for the review. Could you clarify which specific change you are concerned about? The `computeIfAbsent` pattern used here is a standard ConcurrentHashMap atomic operation and is actually the recommended approach for this type of race condition (check-then-act on a map entry). It avoids the performance overhead of synchronized blocks while ensuring thread-safety. Would you prefer a different approach, such as using `synchronized` blocks or accepting the race condition?


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [Bug] Fix TOCTOU race condition in MQClientInstance.brokerVersionTable access [rocketmq]

Posted by "Kris20030907 (via GitHub)" <gi...@apache.org>.
Kris20030907 commented on PR #10235:
URL: https://github.com/apache/rocketmq/pull/10235#issuecomment-4161207978

   @Senrian This change is not recommended as it may cause performance issues. Please refer to the documentation:https://juejin.cn/post/7094561581631012878


-- 
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: commits-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org