Get ClosedChannelException when close channel before SSL handshake
See original GitHub issueHi, recently I encounter a problem when using Netty
and its SlsHandler
. I would like to close a channel before SSL handshake based on my throttling configuration. I try to implement as below, but I will receive ClosedChannelException
when I close the channel.
I add my ThrottleHandler
before SslHandler
in my ChannelInitializer
. It overrides the channelActive
method in order to determine whether we should do the SSL handshake or close the channel before the handshake. The SslHandler
is setting in a server mode.
From my understanding, the SSL handshake should starts when the channel become active, so my closing operation should run before the SSL handshakes starts. However, I still receive a ClosedChannelException
when closing the channel. This exception is triggered in channelInactive
method in SslHandler
. Does the handshake starts when the channel is active, or it just starts when you add SslHandler
to the pipeline?
Expected behavior
The channel should be closed normally without any exception. The handshake should not have started.
Actual behavior
The SslHandler
triggers ClosedChannelException
when I close the channel before the handshake.
java.nio.channels.ClosedChannelException
at io.netty.handler.ssl.SslHandler.channelInactive(...)(Unknown Source) ~[netty-all-4.1.jar:4.1.6.Final]
Steps to reproduce
Normally start a simple server which enables the SSL/TLS feature by adding a SslHandler
into the ChannelInitializer
. The SslHandler
is setup with a Java SSLEngine
in server mode. Then add the ThrottleHandler
(codes are listed below) before SslHandler
.
Minimal yet complete reproducer code (or URL to code)
@ChannelHandler.Sharable
public class ThrottleHandler extends ChannelInboundHandlerAdapter {
private ThrottleConfig config;
private ThrottleConfig config;
public ThrottleHandler(ThrottleConfig config) {
super();
this.config = config;
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (config.isThrottled) {
log.info("Close a connection by throttle handler");
ctx.close();
return;
}
ctx.pipeline().remove(this);
ctx.fireChannelActive();
}
}
public class AbstractChannelInitializer extends ChannelInitializer<SocketChannel> {
private ThrottleHandler throttleHandler;
private SslHandler handshakeHandler;
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(throttleHandler);
// Add Netty SslHandler
channel.pipeline().addLast(handshakeHandler);
// other handlers for business logics
}
}
Netty version
4.1.6.Final
JVM version (e.g. java -version
)
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (6 by maintainers)
Top GitHub Comments
@Jianzhu91 PTAL: https://github.com/netty/netty/pull/7368
Fixed by https://github.com/netty/netty/pull/7368