Skip to content

Commit d939374

Browse files
committed
Fix race in clear scroll (#31259)
Here is the problem: if two threads are racing and one hits a failure freeing a context and the other succeeded, we can expose the value of the has failure marker to the succeeding thread before the failing thread has had a chance to set the failure marker. This is a problem if the failing thread counted down the expected number of operations, then be put to sleep by a gentle lullaby from the OS, and then the other thread could count down to zero. Since the failing thread did not get to set the failure marker, the succeeding thread would respond that the clear scroll succeeded and that makes that thread a liar. This commit addresses by first setting the failure marker before we potentially expose its value to another thread.
1 parent 85f7501 commit d939374

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

server/src/main/java/org/elasticsearch/action/search/ClearScrollController.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,13 @@ private void onFreedContext(boolean freed) {
133133

134134
private void onFailedFreedContext(Throwable e, DiscoveryNode node) {
135135
logger.warn(() -> new ParameterizedMessage("Clear SC failed on node[{}]", node), e);
136+
/*
137+
* 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
138+
* racing thread successfully freeing a context. This would lead to that thread responding that the clear scroll succeeded.
139+
*/
140+
hasFailed.set(true);
136141
if (expectedOps.countDown()) {
137142
listener.onResponse(new ClearScrollResponse(false, freedSearchContexts.get()));
138-
} else {
139-
hasFailed.set(true);
140143
}
141144
}
142145
}

0 commit comments

Comments
 (0)