diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java index 5ba8c8309c6..e59e7245db4 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ConnectHandler.java @@ -329,13 +329,29 @@ public class ConnectHandler extends HandlerWrapper */ protected SocketChannel connect(HttpServletRequest request, String host, int port) throws IOException { - _logger.debug("Establishing connection to {}:{}", host, port); - // Connect to remote server SocketChannel channel = SocketChannel.open(); - channel.socket().setTcpNoDelay(true); - channel.socket().connect(new InetSocketAddress(host, port), getConnectTimeout()); - _logger.debug("Established connection to {}:{}", host, port); - return channel; + try + { + // Connect to remote server + _logger.debug("Establishing connection to {}:{}", host, port); + channel.socket().setTcpNoDelay(true); + channel.socket().connect(new InetSocketAddress(host, port), getConnectTimeout()); + _logger.debug("Established connection to {}:{}", host, port); + return channel; + } + catch (IOException x) + { + _logger.debug("Failed to establish connection to " + host + ":" + port, x); + try + { + channel.close(); + } + catch (IOException xx) + { + Log.ignore(xx); + } + throw x; + } } protected void prepareContext(HttpServletRequest request, ConcurrentMap context)