diff --git a/transport/src/main/java/io/netty/channel/nio/AbstractNioMessageChannel.java b/transport/src/main/java/io/netty/channel/nio/AbstractNioMessageChannel.java index c101872fb3..ffec710d9b 100644 --- a/transport/src/main/java/io/netty/channel/nio/AbstractNioMessageChannel.java +++ b/transport/src/main/java/io/netty/channel/nio/AbstractNioMessageChannel.java @@ -97,11 +97,7 @@ public abstract class AbstractNioMessageChannel extends AbstractNioChannel { pipeline.fireChannelReadComplete(); if (exception != null) { - if (exception instanceof IOException && !(exception instanceof PortUnreachableException)) { - // ServerChannel should not be closed even on IOException because it can often continue - // accepting incoming connections. (e.g. too many open files) - closed = !(AbstractNioMessageChannel.this instanceof ServerChannel); - } + closed = closeOnReadError(exception); pipeline.fireExceptionCaught(exception); } @@ -174,6 +170,14 @@ public abstract class AbstractNioMessageChannel extends AbstractNioChannel { return false; } + protected boolean closeOnReadError(Throwable cause) { + // ServerChannel should not be closed even on IOException because it can often continue + // accepting incoming connections. (e.g. too many open files) + return cause instanceof IOException && + !(cause instanceof PortUnreachableException) && + this instanceof ServerChannel; + } + /** * Read messages into the given array and return the amount which was read. */ diff --git a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java index 73678a5b9e..dc43230a67 100644 --- a/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java +++ b/transport/src/main/java/io/netty/channel/socket/nio/NioDatagramChannel.java @@ -594,4 +594,14 @@ public final class NioDatagramChannel protected void setReadPending(boolean readPending) { super.setReadPending(readPending); } + + @Override + protected boolean closeOnReadError(Throwable cause) { + // We do not want to close on SocketException when using DatagramChannel as we usually can continue receiving. + // See https://github.com/netty/netty/issues/5893 + if (cause instanceof SocketException) { + return false; + } + return super.closeOnReadError(cause); + } }