From d611ab48550180bd735d2f9819f16932dd38b167 Mon Sep 17 00:00:00 2001 From: David Roberts Date: Wed, 10 May 2017 14:59:14 +0100 Subject: [PATCH] Avoid race when shutting down controller processes (#24579) This commit terminates any controller processes plugins might have after the node has been closed. This gives the plugins a chance to shut down their controllers gracefully. Previously there was a race condition where controller processes could be shut down gracefully and terminated by two threads running in parallel, leading to non-deterministic outcomes. Additionally, controller processes that failed to shut down gracefully were not forcibly terminated when running as a Windows service; there was a reliance on the plugin to shut down its controller gracefully in this situation. This commit also fixes this problem. --- .../org/elasticsearch/bootstrap/Bootstrap.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java index 2b47908c352..1e53faa9efc 100644 --- a/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java +++ b/core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java @@ -163,16 +163,6 @@ final class Bootstrap { try { spawner.spawnNativePluginControllers(environment); - Runtime.getRuntime().addShutdownHook(new Thread() { - @Override - public void run() { - try { - spawner.close(); - } catch (IOException e) { - throw new ElasticsearchException("Failed to destroy spawned controllers", e); - } - } - }); } catch (IOException e) { throw new BootstrapException(e); } @@ -191,7 +181,7 @@ final class Bootstrap { @Override public void run() { try { - IOUtils.close(node); + IOUtils.close(node, spawner); LoggerContext context = (LoggerContext) LogManager.getContext(false); Configurator.shutdown(context); } catch (IOException ex) { @@ -269,7 +259,7 @@ final class Bootstrap { static void stop() throws IOException { try { - IOUtils.close(INSTANCE.node); + IOUtils.close(INSTANCE.node, INSTANCE.spawner); } finally { INSTANCE.keepAliveLatch.countDown(); }