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 committed by GitHub
parent 6278d09139
commit 6ed203b7ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -106,7 +106,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.nio.NioEventLoopGroup;
import org.junit.Assert;
@ -45,6 +47,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() {
return new NioServerSocketChannel();