cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From h...@apache.org
Subject git commit: updated refs/heads/bugfix/CID-116538 to de26a72
Date Tue, 16 Sep 2014 14:44:04 GMT
Repository: cloudstack
Updated Branches:
  refs/heads/bugfix/CID-116538 1760a60eb -> de26a7228 (forced update)


Fix resource leaks on exception paths

Make it clear that the server socket isn't a leaked resource


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/de26a722
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/de26a722
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/de26a722

Branch: refs/heads/bugfix/CID-116538
Commit: de26a7228e1298d0a5d4d2c0bf00c924a99cd505
Parents: 50990c4
Author: Hugo Trippaers <htrippaers@schubergphilis.com>
Authored: Tue Sep 16 09:27:05 2014 +0200
Committer: Hugo Trippaers <htrippaers@schubergphilis.com>
Committed: Tue Sep 16 16:43:32 2014 +0200

----------------------------------------------------------------------
 utils/src/com/cloud/utils/nio/Link.java      |  7 +++--
 utils/src/com/cloud/utils/nio/NioClient.java | 36 ++++++++---------------
 utils/src/com/cloud/utils/nio/NioServer.java | 18 +++++++++---
 3 files changed, 31 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/de26a722/utils/src/com/cloud/utils/nio/Link.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/nio/Link.java b/utils/src/com/cloud/utils/nio/Link.java
index 36a8e60..e4291d0 100755
--- a/utils/src/com/cloud/utils/nio/Link.java
+++ b/utils/src/com/cloud/utils/nio/Link.java
@@ -31,6 +31,7 @@ import java.nio.channels.ClosedChannelException;
 import java.nio.channels.ReadableByteChannel;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
+import java.security.GeneralSecurityException;
 import java.security.KeyStore;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentLinkedQueue;
@@ -159,7 +160,7 @@ public class Link {
             pkgBuf.clear();
             engResult = sslEngine.wrap(buffers, pkgBuf);
             if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus()
!= HandshakeStatus.NOT_HANDSHAKING &&
-                engResult.getStatus() != SSLEngineResult.Status.OK) {
+                    engResult.getStatus() != SSLEngineResult.Status.OK) {
                 throw new IOException("SSL: SSLEngine return bad result! " + engResult);
             }
 
@@ -285,7 +286,7 @@ public class Link {
             appBuf = ByteBuffer.allocate(sslSession.getApplicationBufferSize() + 40);
             engResult = _sslEngine.unwrap(_readBuffer, appBuf);
             if (engResult.getHandshakeStatus() != HandshakeStatus.FINISHED && engResult.getHandshakeStatus()
!= HandshakeStatus.NOT_HANDSHAKING &&
-                engResult.getStatus() != SSLEngineResult.Status.OK) {
+                    engResult.getStatus() != SSLEngineResult.Status.OK) {
                 throw new IOException("SSL: SSLEngine return bad result! " + engResult);
             }
             if (remaining == _readBuffer.remaining()) {
@@ -405,7 +406,7 @@ public class Link {
         _connection.scheduleTask(task);
     }
 
-    public static SSLContext initSSLContext(boolean isClient) throws Exception {
+    public static SSLContext initSSLContext(boolean isClient) throws GeneralSecurityException,
IOException {
         InputStream stream;
         SSLContext sslContext = null;
         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/de26a722/utils/src/com/cloud/utils/nio/NioClient.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/nio/NioClient.java b/utils/src/com/cloud/utils/nio/NioClient.java
index 34d03c2..f4b1029 100755
--- a/utils/src/com/cloud/utils/nio/NioClient.java
+++ b/utils/src/com/cloud/utils/nio/NioClient.java
@@ -24,6 +24,7 @@ import java.net.InetSocketAddress;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
+import java.security.GeneralSecurityException;
 
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLEngine;
@@ -48,30 +49,23 @@ public class NioClient extends NioConnection {
     @Override
     protected void init() throws IOException {
         _selector = Selector.open();
-        SocketChannel sch = null;
-        InetSocketAddress addr = null;
+        Task task = null;
 
-        try {
-            sch = SocketChannel.open();
+        try (SocketChannel sch = SocketChannel.open()) {
             sch.configureBlocking(true);
             s_logger.info("Connecting to " + _host + ":" + _port);
 
             if (_bindAddress != null) {
                 s_logger.info("Binding outbound interface at " + _bindAddress);
 
-                addr = new InetSocketAddress(_bindAddress, 0);
-                sch.socket().bind(addr);
+                InetSocketAddress bindAddr = new InetSocketAddress(_bindAddress, 0);
+                sch.socket().bind(bindAddr);
             }
 
-            addr = new InetSocketAddress(_host, _port);
-            sch.connect(addr);
-        } catch (IOException e) {
-            _selector.close();
-            throw e;
-        }
+            InetSocketAddress peerAddr = new InetSocketAddress(_host, _port);
+            sch.connect(peerAddr);
 
-        SSLEngine sslEngine = null;
-        try {
+            SSLEngine sslEngine = null;
             // Begin SSL handshake in BLOCKING mode
             sch.configureBlocking(true);
 
@@ -82,15 +76,10 @@ public class NioClient extends NioConnection {
             Link.doHandshake(sch, sslEngine, true);
             s_logger.info("SSL: Handshake done");
             s_logger.info("Connected to " + _host + ":" + _port);
-        } catch (Exception e) {
-            _selector.close();
-            throw new IOException("SSL: Fail to init SSL! " + e);
-        }
 
-        Task task = null;
-        try {
+
             sch.configureBlocking(false);
-            Link link = new Link(addr, this);
+            Link link = new Link(peerAddr, this);
             link.setSSLEngine(sslEngine);
             SelectionKey key = sch.register(_selector, SelectionKey.OP_READ);
             link.setKey(key);
@@ -98,9 +87,10 @@ public class NioClient extends NioConnection {
             // Notice we've already connected due to the handshake, so let's get the
             // remaining task done
             task = _factory.create(Task.Type.CONNECT, link, null);
-        } catch (Exception e) {
+        } catch (GeneralSecurityException e) {
+            throw new IOException("Failed to initialise security", e);
+        } finally {
             _selector.close();
-            throw new IOException("Fail to init NioClient! " + e);
         }
         _executor.execute(task);
     }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/de26a722/utils/src/com/cloud/utils/nio/NioServer.java
----------------------------------------------------------------------
diff --git a/utils/src/com/cloud/utils/nio/NioServer.java b/utils/src/com/cloud/utils/nio/NioServer.java
index 50c33c8..98a4a51 100755
--- a/utils/src/com/cloud/utils/nio/NioServer.java
+++ b/utils/src/com/cloud/utils/nio/NioServer.java
@@ -33,6 +33,7 @@ public class NioServer extends NioConnection {
     private final static Logger s_logger = Logger.getLogger(NioServer.class);
 
     protected InetSocketAddress _localAddr;
+    private ServerSocketChannel _serverSocket;
 
     protected WeakHashMap<InetSocketAddress, Link> _links;
 
@@ -46,18 +47,27 @@ public class NioServer extends NioConnection {
     protected void init() throws IOException {
         _selector = SelectorProvider.provider().openSelector();
 
-        ServerSocketChannel ssc = ServerSocketChannel.open();
-        ssc.configureBlocking(false);
+        _serverSocket = ServerSocketChannel.open();
+        _serverSocket.configureBlocking(false);
 
         _localAddr = new InetSocketAddress(_port);
-        ssc.socket().bind(_localAddr);
+        _serverSocket.socket().bind(_localAddr);
 
-        ssc.register(_selector, SelectionKey.OP_ACCEPT, null);
+        _serverSocket.register(_selector, SelectionKey.OP_ACCEPT, null);
 
         s_logger.info("NioConnection started and listening on " + _localAddr.toString());
     }
 
     @Override
+    public void cleanUp() throws IOException {
+        super.cleanUp();
+        if (_serverSocket != null) {
+            _serverSocket.close();
+        }
+        s_logger.info("NioConnection stopped on " + _localAddr.toString());
+    }
+
+    @Override
     protected void registerLink(InetSocketAddress addr, Link link) {
         _links.put(addr, link);
     }


Mime
View raw message