This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 7.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/7.0.x by this push: new 0f3c0fb Fix BZ-64951 - Correct potential fd leak when WebSocket connections fail 0f3c0fb is described below commit 0f3c0fbd1c29588463b1c8fddaaea66bd7f0d06c 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 # Conflicts: # java/org/apache/tomcat/websocket/WsWebSocketContainer.java --- .../tomcat/websocket/WsWebSocketContainer.java | 50 +++++++++++----------- webapps/docs/changelog.xml | 9 ++++ 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java index a59296c..087cd04 100644 --- a/java/org/apache/tomcat/websocket/WsWebSocketContainer.java +++ b/java/org/apache/tomcat/websocket/WsWebSocketContainer.java @@ -342,13 +342,13 @@ public class WsWebSocketContainer boolean success = false; List<Extension> extensionsAgreed = new ArrayList<Extension>(); 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); @@ -359,28 +359,20 @@ public class WsWebSocketContainer "wsWebSocketContainer.proxyConnectFail", selectedProxy, Integer.toString(httpResponse.getStatus()))); } - } catch (Exception 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(); @@ -537,7 +529,15 @@ public class WsWebSocketContainer 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 0dd2bd3..18b6eff 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -129,6 +129,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