You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "daguimu (via GitHub)" <gi...@apache.org> on 2026/03/25 11:36:16 UTC

[PR] fix: use ConcurrentHashMap for Version.VERSION2INT to prevent concurrent corruption [dubbo]

daguimu opened a new pull request, #16171:
URL: https://github.com/apache/dubbo/pull/16171

   ## Problem
   
   `Version.VERSION2INT` uses a plain `HashMap` that is accessed concurrently from the RPC hot path:
   
   ```java
   private static final Map<String, Integer> VERSION2INT = new HashMap<>(); // NOT thread-safe
   
   public static int getIntVersion(String version) {
       Integer v = VERSION2INT.get(version);  // concurrent read
       if (v == null) {
           v = parseInt(version);
           VERSION2INT.put(version, v);       // concurrent write
       }
       return v;
   }
   ```
   
   This is called via `isSupportResponseAttachment()` on **every RPC call** to check protocol compatibility.
   
   ### Impact
   
   Concurrent `put()` during `HashMap` internal resize can corrupt the hash table's bucket chain, causing threads to **hang permanently in an infinite loop** during `get()` — a well-documented JDK issue with unsynchronized `HashMap` access. This is a classic production incident pattern that is difficult to diagnose.
   
   ## Root Cause
   
   `HashMap` was used where `ConcurrentHashMap` is 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<>()` with `new ConcurrentHashMap<>()`. The check-then-act pattern in `getIntVersion()` is acceptable because:
   1. The computation is **idempotent** (same version string always produces the same int)
   2. Worst case under concurrent miss: two threads compute and put the same value — no data loss or corruption with `ConcurrentHashMap`
   
   ## Tests Added
   
   - `testGetIntVersionConcurrency` — 10 threads × 1000 iterations calling `getIntVersion` with various version strings concurrently, verifying consistent results
   
   All 9 tests in `VersionTest` pass.
   
   ## Impact
   
   - 1-line production change: `HashMap` → `ConcurrentHashMap`
   - Zero behavioral change
   - Eliminates risk of thread hangs in the RPC protocol path


-- 
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: notifications-unsubscribe@dubbo.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org


Re: [PR] fix: use ConcurrentHashMap for Version.VERSION2INT to prevent concurrent corruption [dubbo]

Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #16171:
URL: https://github.com/apache/dubbo/pull/16171#issuecomment-4126279685

   ## [Codecov](https://app.codecov.io/gh/apache/dubbo/pull/16171?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :white_check_mark: All modified and coverable lines are covered by tests.
   :white_check_mark: Project coverage is 32.23%. Comparing base ([`63fe8c3`](https://app.codecov.io/gh/apache/dubbo/commit/63fe8c3a982a554ba40cd5e62f07d74cebb45e39?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`f992a6a`](https://app.codecov.io/gh/apache/dubbo/commit/f992a6aa00ac65e68f41d7aacc616ddb10e6e3cf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 1 commits behind head on 3.3.
   > :exclamation:  There is a different number of reports uploaded between BASE (63fe8c3) and HEAD (f992a6a). Click for more details.
   > 
   > <details><summary>HEAD has 7 uploads less than BASE</summary>
   >
   >| Flag | BASE (63fe8c3) | HEAD (f992a6a) |
   >|------|------|------|
   >|integration-tests-java8|1|0|
   >|integration-tests-java21|1|0|
   >|unit-tests-java11|1|0|
   >|unit-tests-java25|1|0|
   >|unit-tests-java8|1|0|
   >|unit-tests-java17|1|0|
   >|unit-tests-java21|1|0|
   ></details>
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@              Coverage Diff              @@
   ##                3.3   #16171       +/-   ##
   =============================================
   - Coverage     60.81%   32.23%   -28.59%     
   + Complexity    11765    10228     -1537     
   =============================================
     Files          1953     1952        -1     
     Lines         89118    89096       -22     
     Branches      13444    13360       -84     
   =============================================
   - Hits          54197    28716    -25481     
   - Misses        29364    56169    +26805     
   + Partials       5557     4211     -1346     
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [integration-tests-java21](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [integration-tests-java8](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [samples-tests-java21](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `32.19% <100.00%> (+<0.01%)` | :arrow_up: |
   | [samples-tests-java8](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `29.72% <100.00%> (-0.03%)` | :arrow_down: |
   | [unit-tests-java11](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit-tests-java17](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit-tests-java21](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit-tests-java25](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   | [unit-tests-java8](https://app.codecov.io/gh/apache/dubbo/pull/16171/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `?` | |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   </details>
   
   [:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/apache/dubbo/pull/16171?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
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: notifications-unsubscribe@dubbo.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org