NioServerSocketChannel.isActive() must return false after close() completes. (#9030)

Motivation:

When a Channel was closed its isActive() method must return false.

Modifications:

First check for isOpen() before isBound() as isBound() will continue to return true even after the underyling fd was closed.

Result:

Fixes https://github.com/netty/netty/issues/9026.
This commit is contained in:
Norman Maurer 2019-04-11 18:54:31 +02:00
parent e00e06c037
commit 806dace32d
2 changed files with 22 additions and 1 deletions

View File

@ -117,7 +117,9 @@ public class NioServerSocketChannel extends AbstractNioMessageChannel
@Override
public boolean isActive() {
return javaChannel().socket().isBound();
// As java.nio.ServerSocketChannel.isBound() will continue to return true even after the channel was closed
// we will also need to check if it is open.
return isOpen() && javaChannel().socket().isBound();
}
@Override

View File

@ -15,6 +15,8 @@
*/
package io.netty.channel.socket.nio;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.MultithreadEventLoopGroup;
import io.netty.channel.nio.NioHandler;
@ -47,6 +49,23 @@ public class NioServerSocketChannelTest extends AbstractNioChannelTest<NioServer
}
}
@Test
public void testIsActiveFalseAfterClose() {
NioServerSocketChannel serverSocketChannel = new NioServerSocketChannel();
EventLoopGroup group = new NioEventLoopGroup(1);
try {
group.register(serverSocketChannel).syncUninterruptibly();
Channel channel = serverSocketChannel.bind(new InetSocketAddress(0)).syncUninterruptibly().channel();
Assert.assertTrue(channel.isActive());
Assert.assertTrue(channel.isOpen());
channel.close().syncUninterruptibly();
Assert.assertFalse(channel.isOpen());
Assert.assertFalse(channel.isActive());
} finally {
group.shutdownGracefully();
}
}
@Override
protected NioServerSocketChannel newNioChannel(EventLoopGroup group) {
return new NioServerSocketChannel(group.next(), group);