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: [email protected]

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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to