can netty reliably detect channel close/disconnect?

Christopher Ng picture Christopher Ng · Apr 3, 2012 · Viewed 8.3k times · Source

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.

Answer

Josh Wilson picture Josh Wilson · Oct 9, 2019

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.