title: Shutting down Netty server when client connections are open
body: <p>I'm trying to shutdown a Netty server that has open connections to it, and it just hangs. Here's what I do.</p>
  
  <ul>
  <li>Start server on one machine and client on another.</li>
  <li>Send a message from client to server to which I get a response.</li>
  <li>Shutdown server using Ctrl-C</li>
  </ul>
  
  <p>I've registered a shutdown hook on the server that closes the ChannelGroup and calls releaseExternalResources on the ServerBootstrap (or actually I'm using the DuplexTcpServerBootstrap of the protobuf-pro-duplex library that does just that). Anyway, the shutdown hook is called properly on shutdown, but it never returns. When I take a thread dump of what's happening I can see two interesting stacks:</p>
  
  <pre><code>   java.lang.Thread.State: TIMED_WAITING (parking)
      at sun.misc.Unsafe.park(Native Method)
      - parking to wait for  &lt;0x00000006b0890950&gt; (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
      at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2082)
      at java.util.concurrent.ThreadPoolExecutor.awaitTermination(ThreadPoolExecutor.java:1433)
      at org.jboss.netty.util.internal.ExecutorUtil.terminate(ExecutorUtil.java:103)
      at org.jboss.netty.channel.socket.nio.AbstractNioWorkerPool.releaseExternalResources(AbstractNioWorkerPool.java:80)
      at org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory.releaseExternalResources(NioServerSocketChannelFactory.java:162)
      at org.jboss.netty.bootstrap.Bootstrap.releaseExternalResources(Bootstrap.java:319)
      at com.googlecode.protobuf.pro.duplex.server.DuplexTcpServerBootstrap.releaseExternalResources(DuplexTcpServerBootstrap.java:132)
      at com.xxx.yyy.node.NodeServer$2.run(NodeServer.java:104)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>So this is the shutdown hook thread that never returns. And below is another thread that seems to be waiting on a channel:</p>
  
  <pre><code>   java.lang.Thread.State: RUNNABLE
      at sun.nio.ch.EPollArrayWrapper.interrupt(Native Method)
      at sun.nio.ch.EPollArrayWrapper.interrupt(EPollArrayWrapper.java:274)
      at sun.nio.ch.EPollSelectorImpl.wakeup(EPollSelectorImpl.java:193)
      - locked &lt;0x00000006b0896660&gt; (a java.lang.Object)
      at java.nio.channels.spi.AbstractSelector$1.interrupt(AbstractSelector.java:210)
      at java.nio.channels.spi.AbstractSelector.begin(AbstractSelector.java:216)
      at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:80)
      at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)
      - locked &lt;0x00000006b08964a8&gt; (a sun.nio.ch.Util$2)
      - locked &lt;0x00000006b0896498&gt; (a java.util.Collections$UnmodifiableSet)
      - locked &lt;0x00000006b0890d20&gt; (a sun.nio.ch.EPollSelectorImpl)
      at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)
      at org.jboss.netty.channel.socket.nio.SelectorUtil.select(SelectorUtil.java:52)
      at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:208)
      at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:38)
      at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
      at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>I'm using Netty 3.4.6.Final with Java 7.04 on Linux. Thanks!</p>
  
  <p>Br,
  Frank.</p>