From 85e55eebd894b69d0e67f357671dbd31ba4d8840 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 23 Aug 2012 20:41:22 +0200 Subject: [PATCH 1/2] Add a workaround for the 100% cpu usage that can happen because of an endless loop. See #535 --- .../socket/nio/NioClientSocketPipelineSink.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java index 9bd5032a6d..8abdce5398 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java @@ -20,6 +20,7 @@ import static org.jboss.netty.channel.Channels.*; import java.io.IOException; import java.net.ConnectException; import java.net.SocketAddress; +import java.nio.channels.CancelledKeyException; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; @@ -362,8 +363,17 @@ class NioClientSocketPipelineSink extends AbstractNioChannelSink { continue; } - if (k.isConnectable()) { - connect(k); + try { + if (k.isConnectable()) { + connect(k); + } + } catch (CancelledKeyException t) { + // Catch CancelledKeyException which may get thrown. + NioClientSocketChannel ch = (NioClientSocketChannel) k.attachment(); + ch.connectFuture.setFailure(t); + fireExceptionCaught(ch, t); + k.cancel(); // Some JDK implementations run into an infinite loop without this. + ch.worker.close(ch, succeededFuture(ch)); } } } From 14d01f4a8f4cdfe8bb56f4c456659d0eb0c099b8 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 23 Aug 2012 21:04:34 +0200 Subject: [PATCH 2/2] Move the catch block out of the connect method, which makes more sense as we need to catch it anyway in a more general scope. See See #535 --- .../nio/NioClientSocketPipelineSink.java | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java index 8abdce5398..29f43f6d87 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioClientSocketPipelineSink.java @@ -20,7 +20,6 @@ import static org.jboss.netty.channel.Channels.*; import java.io.IOException; import java.net.ConnectException; import java.net.SocketAddress; -import java.nio.channels.CancelledKeyException; import java.nio.channels.ClosedChannelException; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; @@ -367,8 +366,7 @@ class NioClientSocketPipelineSink extends AbstractNioChannelSink { if (k.isConnectable()) { connect(k); } - } catch (CancelledKeyException t) { - // Catch CancelledKeyException which may get thrown. + } catch (Throwable t) { NioClientSocketChannel ch = (NioClientSocketChannel) k.attachment(); ch.connectFuture.setFailure(t); fireExceptionCaught(ch, t); @@ -408,18 +406,11 @@ class NioClientSocketPipelineSink extends AbstractNioChannelSink { } } - private void connect(SelectionKey k) { + private void connect(SelectionKey k) throws IOException { NioClientSocketChannel ch = (NioClientSocketChannel) k.attachment(); - try { - if (ch.channel.finishConnect()) { - k.cancel(); - ch.worker.register(ch, ch.connectFuture); - } - } catch (Throwable t) { - ch.connectFuture.setFailure(t); - fireExceptionCaught(ch, t); - k.cancel(); // Some JDK implementations run into an infinite loop without this. - ch.worker.close(ch, succeededFuture(ch)); + if (ch.channel.finishConnect()) { + k.cancel(); + ch.worker.register(ch, ch.connectFuture); } }