Skip to content

NioEventLoop takes too long to shut dowm #2545

@vkorenev

Description

@vkorenev

Netty version: 4.0.19.Final

NioEventLoop.shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) takes more than 1 second to shut down even if the specified quietPeriod is very short.

This is a big problem in a project with lots of unit tests.

NioEventLoop thread is blocked in selector.select(timeoutMillis) call at NioEventLoop.java:618

A simple test:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.junit.Test;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.assertTrue;

public class NettyNioTest {
  @Test
  public void testShutdown() throws Exception {
    EventLoopGroup serverEventLoopGroup = new NioEventLoopGroup();
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(serverEventLoopGroup)
            .channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
              @Override
              public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter());
              }
            });

    serverBootstrap.bind(0).sync();

    final long t0 = System.currentTimeMillis();
    serverEventLoopGroup.shutdownGracefully(50, 5000, MILLISECONDS).sync();
    final long t = System.currentTimeMillis() - t0;
    assertTrue("Shutdown took " + t + " ms", t < 200);
  }
}

$ java -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

Operating system: Ubuntu Linux 14.04 64-bit

$ uname -a
Linux quark 3.13.0-29-generic #53-Ubuntu SMP Wed Jun 4 21:00:20 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions