diff --git a/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java b/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java index 1f5c4b0ec7b..4d7fc6b8f5a 100644 --- a/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java +++ b/server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java @@ -2926,9 +2926,14 @@ public class IndexShardTests extends IndexShardTestCase { if (randomBoolean() && searchers.size() > 1) { // Close one of the searchers at random - Engine.Searcher searcher = searchers.remove(0); - logger.debug("--> {} closing searcher {}", threadName, searcher.source()); - IOUtils.close(searcher); + synchronized (searchers) { + // re-check because it could have decremented after the check + if (searchers.size() > 1) { + Engine.Searcher searcher = searchers.remove(0); + logger.debug("--> {} closing searcher {}", threadName, searcher.source()); + IOUtils.close(searcher); + } + } } } catch (Exception e) { logger.warn("--> got exception: ", e);