diff --git a/core/src/main/java/org/elasticsearch/action/search/ClearScrollController.java b/core/src/main/java/org/elasticsearch/action/search/ClearScrollController.java index ac708d9b6b0c7..87276ee1e4eaf 100644 --- a/core/src/main/java/org/elasticsearch/action/search/ClearScrollController.java +++ b/core/src/main/java/org/elasticsearch/action/search/ClearScrollController.java @@ -134,10 +134,13 @@ private void onFreedContext(boolean freed) { private void onFailedFreedContext(Throwable e, DiscoveryNode node) { logger.warn((Supplier) () -> new ParameterizedMessage("Clear SC failed on node[{}]", node), e); + /* + * We have to set the failure marker before we count down otherwise we can expose the failure marker before we have set it to a + * racing thread successfully freeing a context. This would lead to that thread responding that the clear scroll succeeded. + */ + hasFailed.set(true); if (expectedOps.countDown()) { listener.onResponse(new ClearScrollResponse(false, freedSearchContexts.get())); - } else { - hasFailed.set(true); } } }