Skip to content

Commit

Permalink
GH-9694: Remove duplicate SocketChannel initialization
Browse files Browse the repository at this point in the history
Fixes: #9694
Issue link: #9694

- Add null check before closing `socketChannel` in catch block
- Prevents potential `NullPointerException` during error handling
  • Loading branch information
anthologia authored and artembilan committed Dec 6, 2024
1 parent f56f5cf commit 1d66863
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @author Gary Russell
* @author Artem Bilan
* @author Christian Tzolov
* @author Jooyoung Pyoung
*
* @since 2.0
*
Expand Down Expand Up @@ -84,8 +85,9 @@ protected void checkActive() {

@Override
protected TcpConnectionSupport buildNewConnection() {
SocketChannel socketChannel = null;
try {
SocketChannel socketChannel = SocketChannel.open();
socketChannel = SocketChannel.open();
setSocketAttributes(socketChannel.socket());
connect(socketChannel);
TcpNioConnection connection =
Expand All @@ -112,6 +114,14 @@ protected TcpConnectionSupport buildNewConnection() {
return wrappedConnection;
}
catch (IOException e) {
try {
if (socketChannel != null) {
socketChannel.close();
}
}
catch (IOException e2) {
logger.error(e2, "Error closing socket channel");
}
throw new UncheckedIOException(e);
}
catch (InterruptedException e) {
Expand Down

0 comments on commit 1d66863

Please sign in to comment.