netty: Netty causes ClassLoader leak in container environments

I’m running Netty in a container-like environment where modules can be loaded and unloaded. Unloading a module that has used Netty causes the ClassLoader to be leaked.

I’m able to reproduce with this minimal example (running in the container environment, obviously):

public class GatewayHook extends AbstractGatewayModuleHook {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private final NioEventLoopGroup eventLoop = new NioEventLoopGroup(0);

    @Override
    public void setup(GatewayContext gatewayContext) {
        logger.info("setup()");
    }

    @Override
    public void startup(LicenseState licenseState) {
        logger.info("startup()");

        try {
            Bootstrap bootstrap = new Bootstrap();

            bootstrap.group(eventLoop)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        logger.info("initChannel");
                    }
                });

            bootstrap.connect("localhost", 1234).get(2, TimeUnit.SECONDS);
        } catch (Throwable t) {
            logger.error("failed getting un-gettable endpoints: {}", t.getMessage(), t);
        }
    }

    @Override
    public void shutdown() {
        logger.info("shutdown()");

        try {
            eventLoop.shutdownGracefully().get();
        } catch (Throwable e) {
            logger.error("Error waiting for event loop shutdown: {}", e.getMessage(), e);
        }
        try {
            GlobalEventExecutor.INSTANCE.shutdownGracefully().get();
        } catch (Throwable e) {
            logger.error("Error waiting for GlobalEventExecutor shutdown: {}", e.getMessage(), e);
        }
        try {
            DefaultAddressResolverGroup.INSTANCE.close();
        } catch (Throwable e) {
            logger.error("Error closing DefaultAddressResolverGroup: {}", e.getMessage(), e);
        }
        InternalThreadLocalMap.destroy();
        FastThreadLocal.removeAll();
    }

}

I’ve tried shutting down as many global looking Netty resources as I could find but it still leaks. I’ve uploaded a JProfiler heap dump here. If you group by ClassLoader and then select the only NonLockingURLClassLoader instance you’ll see the Netty classes still hanging around.

I have of course tested this without invoking any Netty code and it does not leak.

Netty version

Netty 4.1.9.Final

JVM version (e.g. java -version)

java version “1.8.0_112”

OS version (e.g. uname -a)

macOS 10.12.3

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 21 (11 by maintainers)

Most upvoted comments

@kevinherron go for 4.1.22 (4.0.x has been declared EOL).