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

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/master by this push:
     new 01ad27c  Fix BZ-64951 - Correct potential fd leak when WebSocket 
connections fail
01ad27c is described below

commit 01ad27c568594f32ab178983d97e53b8b3b25d25
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Dec 2 17:27:21 2020 +0000

    Fix BZ-64951 - Correct potential fd leak when WebSocket connections fail
    
    https://bz.apache.org/bugzilla/show_bug.cgi?id=64951
    Patch provided by Maurizio Adami
---
 .../tomcat/websocket/WsWebSocketContainer.java     | 51 +++++++++++-----------
 webapps/docs/changelog.xml                         |  9 ++++
 2 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java 
b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
index 28d0543..e6dc4b3 100644
--- a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
+++ b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
@@ -305,13 +305,13 @@ public class WsWebSocketContainer implements 
WebSocketContainer, BackgroundProce
         boolean success = false;
         List<Extension> extensionsAgreed = new ArrayList<>();
         Transformation transformation = null;
-
-        // Open the connection
-        Future<Void> fConnect = socketChannel.connect(sa);
         AsyncChannelWrapper channel = null;
 
-        if (proxyConnect != null) {
-            try {
+        try {
+            // Open the connection
+            Future<Void> fConnect = socketChannel.connect(sa);
+
+            if (proxyConnect != null) {
                 fConnect.get(timeout, TimeUnit.MILLISECONDS);
                 // Proxy CONNECT is clear text
                 channel = new AsyncChannelWrapperNonSecure(socketChannel);
@@ -322,29 +322,20 @@ public class WsWebSocketContainer implements 
WebSocketContainer, BackgroundProce
                             "wsWebSocketContainer.proxyConnectFail", 
selectedProxy,
                             Integer.toString(httpResponse.getStatus())));
                 }
-            } catch (TimeoutException | InterruptedException | 
ExecutionException |
-                    EOFException e) {
-                if (channel != null) {
-                    channel.close();
-                }
-                throw new DeploymentException(
-                        
sm.getString("wsWebSocketContainer.httpRequestFailed"), e);
             }
-        }
 
-        if (secure) {
-            // Regardless of whether a non-secure wrapper was created for a
-            // proxy CONNECT, need to use TLS from this point on so wrap the
-            // original AsynchronousSocketChannel
-            SSLEngine sslEngine = createSSLEngine(userProperties, host, port);
-            channel = new AsyncChannelWrapperSecure(socketChannel, sslEngine);
-        } else if (channel == null) {
-            // Only need to wrap as this point if it wasn't wrapped to process 
a
-            // proxy CONNECT
-            channel = new AsyncChannelWrapperNonSecure(socketChannel);
-        }
+            if (secure) {
+                // Regardless of whether a non-secure wrapper was created for a
+                // proxy CONNECT, need to use TLS from this point on so wrap 
the
+                // original AsynchronousSocketChannel
+                SSLEngine sslEngine = createSSLEngine(userProperties, host, 
port);
+                channel = new AsyncChannelWrapperSecure(socketChannel, 
sslEngine);
+            } else if (channel == null) {
+                // Only need to wrap as this point if it wasn't wrapped to 
process a
+                // proxy CONNECT
+                channel = new AsyncChannelWrapperNonSecure(socketChannel);
+            }
 
-        try {
             fConnect.get(timeout, TimeUnit.MILLISECONDS);
 
             Future<Void> fHandshake = channel.handshake();
@@ -500,7 +491,15 @@ public class WsWebSocketContainer implements 
WebSocketContainer, BackgroundProce
             throw new 
DeploymentException(sm.getString("wsWebSocketContainer.httpRequestFailed", 
path), e);
         } finally {
             if (!success) {
-                channel.close();
+                if (channel != null) {
+                    channel.close();
+                } else {
+                    try {
+                        socketChannel.close();
+                    } catch (IOException ioe) {
+                        // Ignore
+                    }
+                }
             }
         }
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 3424058..1e4cf3a 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -151,6 +151,15 @@
       </fix>
     </changelog>
   </subsection>
+  <subsection name="WebSocket">
+    <changelog>
+      <fix>
+        <bug>64951</bug>: Fix a potential file descriptor leak when WebSocket
+        connections are attempted and fail. Patch provided by Maurizio Adami.
+        (markt)
+      </fix>
+    </changelog>
+  </subsection>
   <subsection name="Web applications">
     <changelog>
       <fix>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to