Skip to content

Commit 1d66863

Browse files
anthologiaartembilan
authored andcommitted
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
1 parent f56f5cf commit 1d66863

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
@@ -39,6 +39,7 @@
3939
* @author Gary Russell
4040
* @author Artem Bilan
4141
* @author Christian Tzolov
42+
* @author Jooyoung Pyoung
4243
*
4344
* @since 2.0
4445
*
@@ -84,8 +85,9 @@ protected void checkActive() {
8485

8586
@Override
8687
protected TcpConnectionSupport buildNewConnection() {
88+
SocketChannel socketChannel = null;
8789
try {
88-
SocketChannel socketChannel = SocketChannel.open();
90+
socketChannel = SocketChannel.open();
8991
setSocketAttributes(socketChannel.socket());
9092
connect(socketChannel);
9193
TcpNioConnection connection =
@@ -112,6 +114,14 @@ protected TcpConnectionSupport buildNewConnection() {
112114
return wrappedConnection;
113115
}
114116
catch (IOException e) {
117+
try {
118+
if (socketChannel != null) {
119+
socketChannel.close();
120+
}
121+
}
122+
catch (IOException e2) {
123+
logger.error(e2, "Error closing socket channel");
124+
}
115125
throw new UncheckedIOException(e);
116126
}
117127
catch (InterruptedException e) {

0 commit comments

Comments
 (0)