Skip to content

[ISSUE #10207] Fix potential NPE in HA connections when remote address is null#10209

Open
daguimu wants to merge 1 commit intoapache:developfrom
daguimu:fix/ha-connection-npe-10207
Open

[ISSUE #10207] Fix potential NPE in HA connections when remote address is null#10209
daguimu wants to merge 1 commit intoapache:developfrom
daguimu:fix/ha-connection-npe-10207

Conversation

@daguimu
Copy link
Copy Markdown

@daguimu daguimu commented Mar 25, 2026

Problem

In DefaultHAConnection and AutoSwitchHAConnection, .toString() is called directly on the return value of getRemoteSocketAddress() without a null check:

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():

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

…address is null

getRemoteSocketAddress() can return null if the socket is not connected.
Add null check before calling toString() in DefaultHAConnection and
AutoSwitchHAConnection constructors to prevent NullPointerException.

Fixes apache#10207
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Potential NPE in DefaultHAConnection and AutoSwitchHAConnection when getRemoteSocketAddress() returns null

1 participant