Skip to content

Commit 7427d4e

Browse files
authored
GH-9694: Remove duplicate SocketChannel initialization
Fixes: #9694 Issue link: #9694 - Add null check before closing `socketChannel` in catch block - Prevents potential `NullPointerException` during error handling **Auto-cherry-pick to `6.3.x`**
1 parent 5313c42 commit 7427d4e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Artem Bilan
4141
* @author Christian Tzolov
4242
* @author Ngoc Nhan
43+
* @author Jooyoung Pyoung
4344
*
4445
* @since 2.0
4546
*
@@ -85,8 +86,9 @@ protected void checkActive() {
8586

8687
@Override
8788
protected TcpConnectionSupport buildNewConnection() {
89+
SocketChannel socketChannel = null;
8890
try {
89-
SocketChannel socketChannel = SocketChannel.open();
91+
socketChannel = SocketChannel.open();
9092
setSocketAttributes(socketChannel.socket());
9193
connect(socketChannel);
9294
TcpNioConnection connection =
@@ -113,6 +115,14 @@ protected TcpConnectionSupport buildNewConnection() {
113115
return wrappedConnection;
114116
}
115117
catch (IOException e) {
118+
try {
119+
if (socketChannel != null) {
120+
socketChannel.close();
121+
}
122+
}
123+
catch (IOException e2) {
124+
logger.error(e2, "Error closing socket channel");
125+
}
116126
throw new UncheckedIOException(e);
117127
}
118128
catch (InterruptedException e) {

0 commit comments

Comments
 (0)