From fb61cc878f9f15a78eb565122cc4cf82531067d9 Mon Sep 17 00:00:00 2001 From: Hanson Date: Tue, 22 Nov 2016 11:43:26 +0800 Subject: [PATCH] Ensure trying to recover from exceptionCaught on the ServerChannel works as expected Motivation: When "Too many open files" happens,the URLClassLoader cannot do any classloading because URLClassLoader need a FD for findClass. Because of this the anonymous inner class that is created to re-enable auto read may cause a problem. Modification: Pre-create Runnable that is scheduled and so ensure it is not lazy loaded. Result: No more problems when try to recover. --- .../io/netty/bootstrap/ServerBootstrap.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java index 9e7eeab49f..92985bcdf1 100644 --- a/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java +++ b/transport/src/main/java/io/netty/bootstrap/ServerBootstrap.java @@ -176,7 +176,7 @@ public class ServerBootstrap extends AbstractBootstrap() { @Override - public void initChannel(Channel ch) throws Exception { + public void initChannel(final Channel ch) throws Exception { final ChannelPipeline pipeline = ch.pipeline(); ChannelHandler handler = handler(); if (handler != null) { @@ -191,7 +191,7 @@ public class ServerBootstrap extends AbstractBootstrap, Object>[] childOptions; private final Entry, Object>[] childAttrs; + private final Runnable enableAutoReadTask; ServerBootstrapAcceptor( - EventLoopGroup childGroup, ChannelHandler childHandler, + final Channel channel, EventLoopGroup childGroup, ChannelHandler childHandler, Entry, Object>[] childOptions, Entry, Object>[] childAttrs) { this.childGroup = childGroup; this.childHandler = childHandler; this.childOptions = childOptions; this.childAttrs = childAttrs; + + // Task which is scheduled to re-enable auto-read. + // It's important to create this Runnable before we try to submit it as otherwise the URLClassLoader may + // not be able to load the class because of the file limit it already reached. + // + // See https://github.com/netty/netty/issues/1328 + enableAutoReadTask = new Runnable() { + @Override + public void run() { + channel.config().setAutoRead(true); + } + }; } @Override @@ -276,12 +289,7 @@ public class ServerBootstrap extends AbstractBootstrap