Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2053

Default NioEventLoopGroup created by driver must be closed

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.4.0
    • Affects Version/s: 3.2.0
    • Component/s: Async
    • None

      From https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!msg/mongodb-user/ynz9LfmgjV0/igj2MXgXAwAJ

      We're using the async driver in a webapp deployed to Tomcat (v8, along with Java 8). After Tomcat is shut down, its log file (catalina.out) has several entries that read something like this:

      The web application appears to have started a thread named nioEventLoopGroup-2-1 but has failed to stop it. This is very likely to create a memory leak.

      The culprit is com.mongodb.connection.netty.NettyStreamFactoryFactory. It creates an instance of NioEventLoopGroup but never shuts it down.

      We're using Spring and our solution was to write our own StreamFactoryFactory that is identical to NettyStreamFactoryFactory except in two ways. It's annotated with @Component (so its lifecycle is managed by Spring) and it has the following method:

      @PreDestroy
      public void shutdown()
      {
        eventLoopGroup.shutdownGracefully().awaitUniterruptibly();
      }
      

      For those not using Spring I recommend creating doing something like this...

      EventLoopGroup myEventLoopGroup = new NioEventLoopGroup();
      StreamFactoryFactory myStreamFactoryFactory = new NettyStreamFactoryFactory(myEventLoopGroup, ByteBufAllocator.DEFAULT);
      

      ...and shut down myEventLoopGroup yourself at the appropriate time.

      The async driver should properly manage the EventLoopGroup it creates...we had to kill Tomcat manually until we figured out the simple fix.

            Assignee:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Reporter:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: