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

   ## Problem
   
   In `DefaultHAConnection` and `AutoSwitchHAConnection`, `.toString()` is 
called directly on the return value of `getRemoteSocketAddress()` without a 
null check:
   
   ```java
   this.clientAddress = 
this.socketChannel.socket().getRemoteSocketAddress().toString();
   ```
   
   `Socket.getRemoteSocketAddress()` returns `null` if the socket is not 
connected. Under network instability (e.g., slave connects and immediately 
disconnects before the HA connection constructor completes), this throws 
`NullPointerException`.
   
   ## Root Cause
   
   No null check on the return value of `getRemoteSocketAddress()` before 
calling `.toString()`.
   
   ## Fix
   
   Extract the `SocketAddress` into a local variable and check for null before 
calling `.toString()`:
   
   ```java
   SocketAddress remoteAddress = 
this.socketChannel.socket().getRemoteSocketAddress();
   this.clientAddress = remoteAddress != null ? remoteAddress.toString() : "";
   ```
   
   Applied to both:
   - `DefaultHAConnection.java:64`
   - `AutoSwitchHAConnection.java:109`
   
   ## Tests Added
   
   - `testConstructorWithNullRemoteAddress` — Creates a `DefaultHAConnection` 
with a non-connected `SocketChannel` (where `getRemoteSocketAddress()` returns 
null). Verifies no NPE is thrown and `clientAddress` is set to empty string.
   
   ## Impact
   
   - Defensive null check only, no behavioral change for normal connected 
sockets
   - Prevents NPE crash during HA connection setup under network instability
   
   Fixes #10207


-- 
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