diff --git a/src/main/java/org/elasticsearch/index/IndexService.java b/src/main/java/org/elasticsearch/index/IndexService.java index c32dfe4c016..5bd1923681a 100644 --- a/src/main/java/org/elasticsearch/index/IndexService.java +++ b/src/main/java/org/elasticsearch/index/IndexService.java @@ -376,36 +376,40 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone private void closeShardInjector(String reason, ShardId sId, Injector shardInjector, IndexShard indexShard) { final int shardId = sId.id(); try { - indicesLifecycle.beforeIndexShardClosed(sId, indexShard, indexSettings); - for (Class closeable : pluginsService.shardServices()) { - try { - shardInjector.getInstance(closeable).close(); - } catch (Throwable e) { - logger.debug("[{}] failed to clean plugin shard service [{}]", e, shardId, closeable); + try { + indicesLifecycle.beforeIndexShardClosed(sId, indexShard, indexSettings); + } finally { + // close everything else even if the beforeIndexShardClosed threw an exception + for (Class closeable : pluginsService.shardServices()) { + try { + shardInjector.getInstance(closeable).close(); + } catch (Throwable e) { + logger.debug("[{}] failed to clean plugin shard service [{}]", e, shardId, closeable); + } } - } - // now we can close the translog service, we need to close it before the we close the shard - closeInjectorResource(sId, shardInjector, TranslogService.class); - // this logic is tricky, we want to close the engine so we rollback the changes done to it - // and close the shard so no operations are allowed to it - if (indexShard != null) { - try { - final boolean flushEngine = deleted.get() == false && closed.get(); // only flush we are we closed (closed index or shutdown) and if we are not deleted - indexShard.close(reason, flushEngine); - } catch (Throwable e) { - logger.debug("[{}] failed to close index shard", e, shardId); - // ignore + // now we can close the translog service, we need to close it before the we close the shard + closeInjectorResource(sId, shardInjector, TranslogService.class); + // this logic is tricky, we want to close the engine so we rollback the changes done to it + // and close the shard so no operations are allowed to it + if (indexShard != null) { + try { + final boolean flushEngine = deleted.get() == false && closed.get(); // only flush we are we closed (closed index or shutdown) and if we are not deleted + indexShard.close(reason, flushEngine); + } catch (Throwable e) { + logger.debug("[{}] failed to close index shard", e, shardId); + // ignore + } } - } - closeInjectorResource(sId, shardInjector, - MergeSchedulerProvider.class, - MergePolicyProvider.class, - IndexShardGatewayService.class, - Translog.class, - PercolatorQueriesRegistry.class); + closeInjectorResource(sId, shardInjector, + MergeSchedulerProvider.class, + MergePolicyProvider.class, + IndexShardGatewayService.class, + Translog.class, + PercolatorQueriesRegistry.class); - // call this before we close the store, so we can release resources for it - indicesLifecycle.afterIndexShardClosed(sId, indexShard, indexSettings); + // call this before we close the store, so we can release resources for it + indicesLifecycle.afterIndexShardClosed(sId, indexShard, indexSettings); + } } finally { try { shardInjector.getInstance(Store.class).close();