From eb1db1140ba7530bf7e95db55fbe423b0768431b Mon Sep 17 00:00:00 2001 From: kimchy Date: Tue, 1 Feb 2011 23:08:52 +0200 Subject: [PATCH] better to close the channels on another thread as well, just to be safe --- .../transport/netty/NettyTransport.java | 84 +++++++++---------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java b/modules/elasticsearch/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java index 22b6e434c8f..a587650446b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/transport/netty/NettyTransport.java @@ -302,61 +302,55 @@ public class NettyTransport extends AbstractLifecycleComponent implem } @Override protected void doStop() throws ElasticSearchException { - if (serverChannel != null) { - try { - serverChannel.close().awaitUninterruptibly(); - } finally { - serverChannel = null; - } - } + final CountDownLatch latch = new CountDownLatch(1); + // make sure we run it on another thread than a possible IO handler thread + threadPool.cached().execute(new Runnable() { + @Override public void run() { + try { + for (Iterator it = connectedNodes.values().iterator(); it.hasNext();) { + NodeChannels nodeChannels = it.next(); + it.remove(); + nodeChannels.close(); + } - if (serverOpenChannels != null) { - serverOpenChannels.close(); - serverOpenChannels = null; - } + if (serverChannel != null) { + try { + serverChannel.close().awaitUninterruptibly(); + } finally { + serverChannel = null; + } + } - if (serverBootstrap != null) { - final CountDownLatch latch = new CountDownLatch(1); - threadPool.cached().execute(new Runnable() { - @Override public void run() { - try { + if (serverOpenChannels != null) { + serverOpenChannels.close(); + serverOpenChannels = null; + } + + if (serverBootstrap != null) { serverBootstrap.releaseExternalResources(); - } finally { - latch.countDown(); + serverBootstrap = null; } - } - }); - try { - latch.await(30, TimeUnit.SECONDS); - } catch (InterruptedException e) { - // ignore - } - serverBootstrap = null; - } - for (Iterator it = connectedNodes.values().iterator(); it.hasNext();) { - NodeChannels nodeChannels = it.next(); - it.remove(); - nodeChannels.close(); - } + for (Iterator it = connectedNodes.values().iterator(); it.hasNext();) { + NodeChannels nodeChannels = it.next(); + it.remove(); + nodeChannels.close(); + } - if (clientBootstrap != null) { - final CountDownLatch latch = new CountDownLatch(1); - threadPool.cached().execute(new Runnable() { - @Override public void run() { - try { + if (clientBootstrap != null) { clientBootstrap.releaseExternalResources(); - } finally { - latch.countDown(); + clientBootstrap = null; } + } finally { + latch.countDown(); } - }); - try { - latch.await(30, TimeUnit.SECONDS); - } catch (InterruptedException e) { - // ignore } - clientBootstrap = null; + }); + + try { + latch.await(30, TimeUnit.SECONDS); + } catch (InterruptedException e) { + // ignore } }