Christopher Ng
Christopher Ng

Reputation: 153

can netty reliably detect channel close/disconnect?

specifically i am interested in the case where the other end of the channel dies unexpectedly (eg the process gets killed). it seems that netty does not reliably fire a channel closed/disconnect event, sometimes it does and sometimes it does not. it also does not necessarily throw a SocketException for connection reset.

for what it's worth, i have also tried writing to the channel but this also continues to work without throwing exceptions or firing any other events.

i am testing this on windows, with netty 3.3.0.Final, if that makes any difference.

Upvotes: 12

Views: 8770

Answers (2)

Manfredovik
Manfredovik

Reputation: 138

Try using the method channelUnregistered from the ChannelInboundHandlerAdapter class.

public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
  ctx.fireChannelUnregistered();
  //Your logic goes here...
}

Upvotes: 0

Josh Wilson
Josh Wilson

Reputation: 3745

If a channel disconnects without sending close there is no way to detect it. Usually the best way to detect a disconnect if needed is to periodically send heartbeat events over the channel ever x seconds. Using IdleStateHandler, if you don't receive the heartbeat after the heartbeat + a delta you force disconnect the channel.

Upvotes: 1

Related Questions