IGNITE-20740 Fix SocketChannel leak when connection fails (#11016)
Co-authored-by: Pavel Tupitsyn <ptupitsyn@apache.org>
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java
index 8951e0f..e81c271 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/client/thin/io/gridnioserver/GridNioClientConnectionMultiplexer.java
@@ -146,8 +146,31 @@
rwLock.readLock().lock();
try {
- SocketChannel ch = SocketChannel.open();
- ch.socket().connect(new InetSocketAddress(addr.getHostName(), addr.getPort()), connTimeout);
+ SocketChannel ch = null;
+ try {
+ ch = SocketChannel.open();
+ ch.socket().connect(new InetSocketAddress(addr.getHostName(), addr.getPort()), connTimeout);
+ }
+ catch (Exception e) {
+ if (ch != null) {
+ if (ch.socket() != null) {
+ try {
+ ch.socket().close();
+ }
+ catch (Exception ignored) {
+ // ignore close exception
+ }
+ }
+
+ try {
+ ch.close();
+ }
+ catch (Exception ignored) {
+ // ignore close exception
+ }
+ }
+ throw new ClientConnectionException(e.getMessage(), e);
+ }
Map<Integer, Object> meta = new HashMap<>();
GridNioFuture<?> sslHandshakeFut = null;