From d292fdafddafb749f5b5f077f5cfb00d14ae536a Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 23 Apr 2013 22:42:47 +0900 Subject: [PATCH] Make sure to close the accept channel which couldn't be initialized or added to the message buffer --- .../socket/nio/NioServerSocketChannel.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java index 4a5407e353..b9028a70d1 100755 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioServerSocketChannel.java @@ -22,6 +22,8 @@ import io.netty.channel.ChannelMetadata; import io.netty.channel.nio.AbstractNioMessageChannel; import io.netty.channel.socket.DefaultServerSocketChannelConfig; import io.netty.channel.socket.ServerSocketChannelConfig; +import io.netty.util.internal.logging.InternalLogger; +import io.netty.util.internal.logging.InternalLoggerFactory; import java.io.IOException; import java.net.InetSocketAddress; @@ -39,6 +41,8 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel private static final ChannelMetadata METADATA = new ChannelMetadata(BufType.MESSAGE, false); + private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class); + private static ServerSocketChannel newSocket() { try { return ServerSocketChannel.open(); @@ -106,11 +110,23 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel @Override protected int doReadMessages(MessageBuf buf) throws Exception { SocketChannel ch = javaChannel().accept(); - if (ch == null) { - return 0; + + try { + if (ch != null) { + buf.add(new NioSocketChannel(this, null, ch)); + return 1; + } + } catch (Throwable t) { + logger.warn("Failed to create a new channel from an accepted socket.", t); + + try { + ch.close(); + } catch (Throwable t2) { + logger.warn("Failed to close a socket.", t2); + } } - buf.add(new NioSocketChannel(this, null, ch)); - return 1; + + return 0; } // Unnecessary stuff