Make sure to close the accept channel which couldn't be initialized or added to the message buffer

This commit is contained in:
Trustin Lee 2013-04-23 22:42:47 +09:00
parent cc0ad9f1cc
commit d292fdafdd

View File

@ -22,6 +22,8 @@ import io.netty.channel.ChannelMetadata;
import io.netty.channel.nio.AbstractNioMessageChannel; import io.netty.channel.nio.AbstractNioMessageChannel;
import io.netty.channel.socket.DefaultServerSocketChannelConfig; import io.netty.channel.socket.DefaultServerSocketChannelConfig;
import io.netty.channel.socket.ServerSocketChannelConfig; 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.io.IOException;
import java.net.InetSocketAddress; 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 ChannelMetadata METADATA = new ChannelMetadata(BufType.MESSAGE, false);
private static final InternalLogger logger = InternalLoggerFactory.getInstance(NioServerSocketChannel.class);
private static ServerSocketChannel newSocket() { private static ServerSocketChannel newSocket() {
try { try {
return ServerSocketChannel.open(); return ServerSocketChannel.open();
@ -106,12 +110,24 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel
@Override @Override
protected int doReadMessages(MessageBuf<Object> buf) throws Exception { protected int doReadMessages(MessageBuf<Object> buf) throws Exception {
SocketChannel ch = javaChannel().accept(); SocketChannel ch = javaChannel().accept();
if (ch == null) {
return 0; try {
} if (ch != null) {
buf.add(new NioSocketChannel(this, null, ch)); buf.add(new NioSocketChannel(this, null, ch));
return 1; 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);
}
}
return 0;
}
// Unnecessary stuff // Unnecessary stuff
@Override @Override