title: Deadlock while closing Netty channel
body: <p>I'm working on an application that uses Netty to send/receive RTP traffic. During a load test, we noticed that our single work thread ran into a deadlock while attempting to wait for Netty to close a channel.</p>
  <p>Context:</p>
  <ul>
  <li>There is a shared <code>NioEventLoopGroup</code> among all RTP Endpoints (=&gt; UDP sockets)</li>
  <li>The channel might get closed while there is still traffic being received</li>
  <li>The closing of the channel definitely happens outside of the Netty threads from a dedicated worker thread</li>
  <li>Netty version is 4.1.65</li>
  </ul>
  <p>Bootstrapping code:</p>
  <pre><code>EventLoopGroup eventLoopGroup = RtpEventLoopGroup.acquireEventLoopGroup();
  Bootstrap b = new Bootstrap();
  b.group(eventLoopGroup);
  b.channel(NioDatagramChannel.class);
  b.option(ChannelOption.SO_BROADCAST, true);
  b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
  b.handler(new ChannelInitializer&lt;DatagramChannel&gt;() {
      @Override
      public void initChannel(final DatagramChannel ch) {
          ch.pipeline().addLast(new RtpInboundChannelHandler(rtpReceiver));
      }
  });
  Channel channel = b.bind(localAddress).syncUninterruptibly().channel();
  </code></pre>
  <p>Shutdown code:</p>
  <pre><code>channel.disconnect().syncUninterruptibly();
  channel.close().syncUninterruptibly();
  RtpEventLoopGroup.releaseEventLoopGroup();
  </code></pre>
  <p>Thread dump of the stuck worker thread (<code>NettyDatagramSocket:107</code> points to the <code>channel.close()</code> method):</p>
  <pre><code>Worker Thread
    at java.lang.Object.wait(J)V (Native Method)
    at java.lang.Object.wait()V (Object.java:328)
    at io.netty.util.concurrent.DefaultPromise.awaitUninterruptibly()Lio/netty/util/concurrent/Promise; (DefaultPromise.java:275)
    at io.netty.channel.DefaultChannelPromise.awaitUninterruptibly()Lio/netty/channel/ChannelPromise; (DefaultChannelPromise.java:137)
    at io.netty.channel.DefaultChannelPromise.awaitUninterruptibly()Lio/netty/util/concurrent/Promise; (DefaultChannelPromise.java:30)
    at io.netty.util.concurrent.DefaultPromise.syncUninterruptibly()Lio/netty/util/concurrent/Promise; (DefaultPromise.java:411)
    at io.netty.channel.DefaultChannelPromise.syncUninterruptibly()Lio/netty/channel/ChannelPromise; (DefaultChannelPromise.java:125)
    at io.netty.channel.DefaultChannelPromise.syncUninterruptibly()Lio/netty/channel/ChannelFuture; (DefaultChannelPromise.java:30)
    at mypackage.NettyDatagramSocket.close()V (NettyDatagramSocket.java:107)
  </code></pre>
  <p>There are 8 NIO EventLoopGroup threads, all are [alive, runnable]</p>
  <ul>
  <li>Some are currently processing a received packet</li>
  <li>None of them seems to be handling the Channel closing</li>
  <li>Some show the following thread stack:</li>
  </ul>
  <pre><code>NIO EventLoopGroup-1
    at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll0(JI[I[I[IJJ)I (Native Method)
    at sun.nio.ch.WindowsSelectorImpl$SubSelector.poll()I (WindowsSelectorImpl.java:357)
    at sun.nio.ch.WindowsSelectorImpl.doSelect(Ljava/util/function/Consumer;J)I (WindowsSelectorImpl.java:182)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect(Ljava/util/function/Consumer;J)I (SelectorImpl.java:124)
    at sun.nio.ch.SelectorImpl.select()I (SelectorImpl.java:141)
    at io.netty.channel.nio.SelectedSelectionKeySetSelector.select()I (SelectedSelectionKeySetSelector.java:68)
    at io.netty.channel.nio.NioEventLoop.select(J)I (NioEventLoop.java:810)
    at io.netty.channel.nio.NioEventLoop.run()V (NioEventLoop.java:457)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run()V (SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run()V (ThreadExecutorMap.java:74)
    at java.lang.Thread.run()V (Thread.java:829)
  </code></pre>
  <p>Does this seem like an error in my application or can I asume this is a bug in Netty? Is there anything else that could be relevant and I might have missed?</p>