0

Strange Netty behavior: Got a server channel that expects heartbeats and catches timeouts with IdleStateHandler. Construction like so:

@Override
public void initChannel(SocketChannel ch) throws Exception { 
    final ByteBuf delimiter = Unpooled.buffer(1); 
    long serverTimeout = 1000; 

    // Break input by newlines: 
    delimiter.writeByte('\n'); 
    ch.pipeline().addLast(new DelimiterBasedFrameDecoder(32768, delimiter)); 

    // Use UTF8 string: 
    ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8)); 

    // Timeout
    ch.pipeline().addLast("idleStateHandler", new IdleStateHandler(serverTimeout, 0, 0, TimeUnit.MILLISECONDS)); 

    // Handle commands: 
    ch.pipeline().addLast("myHandler", new DBConnectionHandler()); 
}

Everything works fine if the client fails to send heartbeats -- timeout is triggered and handled. However, if I brutally kill the client, equivalent of disconnecting the cable, Netty doesn't trigger the timeout.

Using the Netty's timeout exception (instead of idle event) has the same result.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
stanga
  • 2,494
  • 2
  • 17
  • 12

1 Answers1

0

Turns out Netty does recognize the channel drop, it just doesn't trigger a timeout for a dead channel. To detect this call channelHandlerContext.isRemoved() for the relevant channel context.

stanga
  • 2,494
  • 2
  • 17
  • 12