From 4c8d27fc8d7d63ff8e19b5fc0b65c82056b0c6bd Mon Sep 17 00:00:00 2001 From: Jooyoung Pyoung Date: Fri, 6 Dec 2024 03:16:20 +0900 Subject: [PATCH 1/2] GH-9694: Remove duplicate SocketChannel initialization --- .../ip/tcp/connection/TcpNioClientConnectionFactory.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java index 00954ebc05b..c4d9320f3ed 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java @@ -40,6 +40,7 @@ * @author Artem Bilan * @author Christian Tzolov * @author Ngoc Nhan + * @author Jooyoung Pyoung * * @since 2.0 * @@ -85,8 +86,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 = @@ -113,6 +115,11 @@ protected TcpConnectionSupport buildNewConnection() { return wrappedConnection; } catch (IOException e) { + try { + socketChannel.close(); + } catch (IOException e2) { + logger.error(e2, "Error closing socket channel"); + } throw new UncheckedIOException(e); } catch (InterruptedException e) { From 6efba1d28ac256e297a84c9879453a794cab7ee6 Mon Sep 17 00:00:00 2001 From: Jooyoung Pyoung Date: Fri, 6 Dec 2024 15:58:40 +0900 Subject: [PATCH 2/2] GH-9694: Add null check when closing SocketChannel Fixes: #9694 Issue link: https://github.com/spring-projects/spring-integration/issues/9694 - Add null check before closing socketChannel in catch block - Prevents potential NullPointerException during error handling --- .../ip/tcp/connection/TcpNioClientConnectionFactory.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java index c4d9320f3ed..83a06f969af 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java @@ -116,8 +116,11 @@ protected TcpConnectionSupport buildNewConnection() { } catch (IOException e) { try { - socketChannel.close(); - } catch (IOException e2) { + if (socketChannel != null) { + socketChannel.close(); + } + } + catch (IOException e2) { logger.error(e2, "Error closing socket channel"); } throw new UncheckedIOException(e);