question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get ClosedChannelException when close channel before SSL handshake

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

java - Channel closed when running simple example with TLS
As it turns out, I was using incompatible versions of gRPC and BoringSSL. The "Troubleshooting" section of this document contains a table ...
Read more >
Can we ignore ClosedChannelException when writing to ...
and the client (Chrome) close the connection (an Ajax request), this results into this exception on the server: java.nio.channels.ClosedChannelException ...
Read more >
java.nio.channels.ClosedChannelException - org.xnio.ssl ...
channels.ClosedChannelException - org.xnio.ssl.JsseSslConduitEngine.handleHandshake with EJB client trying to connect with SSL to JBoss EAP 7.
Read more >
SslHandler (Netty API Reference (4.0.56.Final))
One exception is when you close the Channel - SslHandler intercepts the close request and send the close_notify message before the channel closure ......
Read more >
SocketChannel (Java Platform SE 7 ) - Oracle Help Center
Socket channels support non-blocking connection: A socket channel may be ... is established then the socket remains bound until the channel is closed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found