You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by jf...@apache.org on 2020/02/12 11:04:02 UTC

[plc4x] branch rel/0.6 updated: Fixed a Situation where Tcp Connections could Leak Sockets.

This is an automated email from the ASF dual-hosted git repository.

jfeinauer pushed a commit to branch rel/0.6
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/rel/0.6 by this push:
     new 25ccea2  Fixed a Situation where Tcp Connections could Leak Sockets.
25ccea2 is described below

commit 25ccea229607bae979316bb6a2ed080313ec6cff
Author: julian <j....@pragmaticminds.de>
AuthorDate: Wed Feb 12 12:03:47 2020 +0100

    Fixed a Situation where Tcp Connections could Leak Sockets.
---
 .../tcp/connection/TcpSocketChannelFactory.java    | 43 ++++++++++------------
 1 file changed, 19 insertions(+), 24 deletions(-)

diff --git a/plc4j/protocols/driver-bases/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java b/plc4j/protocols/driver-bases/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java
index ce21c61..0ab4f15 100644
--- a/plc4j/protocols/driver-bases/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java
+++ b/plc4j/protocols/driver-bases/tcp/src/main/java/org/apache/plc4x/java/tcp/connection/TcpSocketChannelFactory.java
@@ -27,6 +27,7 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.nio.NioSocketChannel;
 import io.netty.util.concurrent.Future;
 import io.netty.util.concurrent.GenericFutureListener;
+import org.apache.plc4x.java.api.PlcConnection;
 import org.apache.plc4x.java.api.exceptions.PlcConnectionException;
 import org.apache.plc4x.java.api.exceptions.PlcException;
 import org.apache.plc4x.java.base.connection.ChannelFactory;
@@ -54,37 +55,31 @@ public class TcpSocketChannelFactory implements ChannelFactory {
     @Override
     public Channel createChannel(ChannelHandler channelHandler)
         throws PlcConnectionException {
+        final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
+
+        Bootstrap bootstrap = new Bootstrap();
+        bootstrap.group(workerGroup);
+        bootstrap.channel(NioSocketChannel.class);
+        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
+        bootstrap.option(ChannelOption.TCP_NODELAY, true);
+        // TODO we should use an explicit (configurable?) timeout here
+        // bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
+        bootstrap.handler(channelHandler);
+        // Start the client.
         try {
-            final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
-
-            Bootstrap bootstrap = new Bootstrap();
-            bootstrap.group(workerGroup);
-            bootstrap.channel(NioSocketChannel.class);
-            bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
-            bootstrap.option(ChannelOption.TCP_NODELAY, true);
-            // TODO we should use an explicit (configurable?) timeout here
-            // bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
-            bootstrap.handler(channelHandler);
-            // Start the client.
+            logger.trace("Starting connection attempt on tcp layer to {}:{}", address.getHostAddress(), port);
             final ChannelFuture f = bootstrap.connect(address, port);
-            f.addListener(new GenericFutureListener<Future<? super Void>>() {
-                @Override public void operationComplete(Future<? super Void> future) throws Exception {
-                    if (!future.isSuccess()) {
-                        logger.info("Unable to connect, shutting down worker thread.");
-                        workerGroup.shutdownGracefully();
-                    }
-                }
-            });
             // Wait for sync
+
             f.sync();
-            f.awaitUninterruptibly(); // jf: unsure if we need that
             // Wait till the session is finished initializing.
             return f.channel();
-        } catch (InterruptedException e) {
-            Thread.currentThread().interrupt();
-            throw new PlcConnectionException("Error creating channel.", e);
         } catch (Exception e) {
-            throw new PlcConnectionException("Error creating channel.", e);
+            // Shutdown worker group here and wait for it
+            logger.info("Unable to connect, shutting down worker thread.");
+            workerGroup.shutdownGracefully().awaitUninterruptibly();
+            logger.debug("Worker Group is shutdown successfully.");
+            throw new PlcConnectionException("Unable to Connect on TCP Layer to " + address.getHostAddress() + ":" + port, e);
         }
     }