'How to correctly close netty channel without workgroup termination

I have following binding to handle UDP packets

private void doStartServer() {
        final UDPPacketHandler udpPacketHandler = new UDPPacketHandler(messageDecodeHandler);
        workerGroup = new NioEventLoopGroup(threadPoolSize);

        try {
            final Bootstrap bootstrap = new Bootstrap();
            bootstrap
                .group(workerGroup)
                .handler(new LoggingHandler(nettyLevel))
                .channel(NioDatagramChannel.class)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(udpPacketHandler);

            bootstrap
                .bind(serverIp, serverPort)
                .sync()
                .channel()
                .closeFuture()
                .await();
        } finally {
            stop();
        }
    }

and handler

@ChannelHandler.Sharable    << note this
@Slf4j
@AllArgsConstructor
public class UDPPacketHandler extends SimpleChannelInboundHandler<DatagramPacket> {

    private final MessageP54Handler messageP54Handler;

    @Override
    public void channelReadComplete(final ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
        log.error("Exception in UDP handler", cause);
        ctx.close();
    }
}

At some point I get this exception java.net.SocketException: Network dropped connection on reset: no further information which is handled in exceptionCaught. This triggers ChannelHandlerContext to close. And at this point whole my server stops (executing on finally block from first snippet)

How to correctly handle exception so that I can handle new connections even after such exception occurs?



Solution 1:[1]

you shouldn't close the ChannelHandlerContext on an IOException when using a DatagramChannel. As DatagramChannel is "connection-less" the exception is specific to one "receive" or one "send" operation. So just log it (or whatever you want to do) and move on.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Norman Maurer