diff --git a/server/src/main/java/org/elasticsearch/threadpool/ThreadPool.java b/server/src/main/java/org/elasticsearch/threadpool/ThreadPool.java index 214cd1f8ced31..4e36c8814e6a7 100644 --- a/server/src/main/java/org/elasticsearch/threadpool/ThreadPool.java +++ b/server/src/main/java/org/elasticsearch/threadpool/ThreadPool.java @@ -621,13 +621,15 @@ public void shutdown() { scheduler.shutdown(); for (ExecutorHolder executor : executors.values()) { if (executor.executor() instanceof ThreadPoolExecutor) { - this.observers.get(executor.info.getName()).forEach((k, v) -> { - try { - v.close(); - } catch (Exception e) { - logger.warn(format("Failed to close LongGaugeObserver for %s. %s", executor.info.getName(), e.getMessage())); - } - }); + if (this.observers.containsKey(executor.info.getName())) { + this.observers.get(executor.info.getName()).forEach((k, v) -> { + try { + v.close(); + } catch (Exception e) { + logger.warn(format("Failed to close LongGaugeObserver for %s. %s", executor.info.getName(), e.getMessage())); + } + }); + } this.observers.remove(executor.info.getName()); executor.executor().shutdown(); } @@ -639,13 +641,15 @@ public void shutdownNow() { scheduler.shutdownNow(); for (ExecutorHolder executor : executors.values()) { if (executor.executor() instanceof ThreadPoolExecutor) { - this.observers.get(executor.info.getName()).forEach((k, v) -> { - try { - v.close(); - } catch (Exception e) { - logger.warn(format("Failed to close LongGaugeObserver for %s. %s", executor.info.getName(), e.getMessage())); - } - }); + if (this.observers.containsKey(executor.info.getName())) { + this.observers.get(executor.info.getName()).forEach((k, v) -> { + try { + v.close(); + } catch (Exception e) { + logger.warn(format("Failed to close LongGaugeObserver for %s. %s", executor.info.getName(), e.getMessage())); + } + }); + } executor.executor().shutdownNow(); } } @@ -655,13 +659,15 @@ public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedE boolean result = scheduler.awaitTermination(timeout, unit); for (ExecutorHolder executor : executors.values()) { if (executor.executor() instanceof ThreadPoolExecutor) { - this.observers.get(executor.info.getName()).forEach((k, v) -> { - try { - v.close(); - } catch (Exception e) { - logger.warn(format("Failed to close LongGaugeObserver for %s. %s", executor.info.getName(), e.getMessage())); - } - }); + if (this.observers.containsKey(executor.info.getName())) { + this.observers.get(executor.info.getName()).forEach((k, v) -> { + try { + v.close(); + } catch (Exception e) { + logger.warn(format("Failed to close LongGaugeObserver for %s. %s", executor.info.getName(), e.getMessage())); + } + }); + } result &= executor.executor().awaitTermination(timeout, unit); } }