Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2014-01-07 21:17:07 +0000
committerJoakim Erdfelt2014-01-07 21:17:07 +0000
commitbb951aeb8bbe0bbfb6bec1a2540505ad5b76a6f4 (patch)
tree913801a368853f530b3421cf07b943dae179e725
parentdfb46350f5fe544e8cf522b6a92489a43e0fcddd (diff)
downloadorg.eclipse.jetty.project-bb951aeb8bbe0bbfb6bec1a2540505ad5b76a6f4.tar.gz
org.eclipse.jetty.project-bb951aeb8bbe0bbfb6bec1a2540505ad5b76a6f4.tar.xz
org.eclipse.jetty.project-bb951aeb8bbe0bbfb6bec1a2540505ad5b76a6f4.zip
425038 - WebSocketClient leaks file handles when exceptions are thrown from open()
-rw-r--r--jetty-util/src/main/java/org/eclipse/jetty/util/IO.java19
-rw-r--r--jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java39
2 files changed, 49 insertions, 9 deletions
diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java b/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java
index ea9ca1d6ae..bc253bcc37 100644
--- a/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java
+++ b/jetty-util/src/main/java/org/eclipse/jetty/util/IO.java
@@ -18,6 +18,7 @@
package org.eclipse.jetty.util;
import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@@ -359,6 +360,24 @@ public class IO
/* ------------------------------------------------------------ */
/**
+ * closes any {@link Closeable}
+ *
+ * @param c the closeable to close
+ */
+ public static void close(Closeable c)
+ {
+ try
+ {
+ if (c != null)
+ c.close();
+ }
+ catch (IOException e)
+ {
+ LOG.ignore(e);
+ }
+ }
+
+ /**
* closes an input stream, and logs exceptions
*
* @param is the input stream to close
diff --git a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java
index 50662050d3..dff6a2fc72 100644
--- a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java
+++ b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java
@@ -35,6 +35,7 @@ import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.log.Logger;
@@ -338,18 +339,38 @@ public class WebSocketClient
InetSocketAddress address = toSocketAddress(uri);
- SocketChannel channel = SocketChannel.open();
- if (_bindAddress != null)
- channel.socket().bind(_bindAddress);
- channel.socket().setTcpNoDelay(true);
+ SocketChannel channel = null;
+ try
+ {
+ channel = SocketChannel.open();
+ if (_bindAddress != null)
+ channel.socket().bind(_bindAddress);
+ channel.socket().setTcpNoDelay(true);
- WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel);
+ WebSocketFuture holder = new WebSocketFuture(websocket,uri,this,channel);
- channel.configureBlocking(false);
- channel.connect(address);
- _factory.getSelectorManager().register(channel, holder);
+ channel.configureBlocking(false);
+ channel.connect(address);
+ _factory.getSelectorManager().register(channel,holder);
+
+ return holder;
+ }
+ catch (RuntimeException e)
+ {
+ // close the channel (prevent connection leak)
+ IO.close(channel);
+
+ // rethrow
+ throw e;
+ }
+ catch(IOException e)
+ {
+ // close the channel (prevent connection leak)
+ IO.close(channel);
- return holder;
+ // rethrow
+ throw e;
+ }
}
public static InetSocketAddress toSocketAddress(URI uri)

Back to the top