Skip to content

Commit 108a6d6

Browse files
committed
Remove ban tasks with the current thread context (elastic#55404)
If the task runs with a user, and it's canceled after we have sent ban requests, then the unban request will be denied as it must not execute with a user. We need to wrap it with the current thread context.
1 parent 96e0491 commit 108a6d6

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/node/tasks/cancel/TransportCancelTasksAction.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package org.elasticsearch.action.admin.cluster.node.tasks.cancel;
2121

2222
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
23+
import org.elasticsearch.ElasticsearchSecurityException;
24+
import org.elasticsearch.ExceptionsHelper;
2325
import org.elasticsearch.ResourceNotFoundException;
2426
import org.elasticsearch.action.ActionListener;
2527
import org.elasticsearch.action.FailedNodeException;
@@ -117,7 +119,11 @@ protected synchronized void taskOperation(CancelTasksRequest request, Cancellabl
117119
final boolean canceled;
118120
if (cancellableTask.shouldCancelChildrenOnCancellation()) {
119121
DiscoveryNodes childNodes = clusterService.state().nodes();
120-
final BanLock banLock = new BanLock(childNodes.getSize(), () -> removeBanOnNodes(cancellableTask, childNodes));
122+
// If the task runs with a user, and it's cancelled after we have sent ban requests, then the unban request
123+
// will be denied as it must not execute with a user. We need to wrap it with the current thread context.
124+
final Runnable removeBans = transportService.getThreadPool().getThreadContext()
125+
.preserveContext(() -> removeBanOnNodes(cancellableTask, childNodes));
126+
final BanLock banLock = new BanLock(childNodes.getSize(), removeBans);
121127
canceled = taskManager.cancel(cancellableTask, request.getReason(), banLock::onTaskFinished);
122128
if (canceled) {
123129
// /In case the task has some child tasks, we need to wait for until ban is set on all nodes
@@ -190,6 +196,7 @@ public void handleResponse(TransportResponse.Empty response) {
190196

191197
@Override
192198
public void handleException(TransportException exp) {
199+
assert ExceptionsHelper.unwrapCause(exp) instanceof ElasticsearchSecurityException == false;
193200
logger.warn("Cannot send ban for tasks with the parent [{}] to the node [{}]", request.parentTaskId, node.key);
194201
listener.onFailure(exp);
195202
}
@@ -200,8 +207,14 @@ public void handleException(TransportException exp) {
200207
private void sendRemoveBanRequest(DiscoveryNodes nodes, BanParentTaskRequest request) {
201208
for (ObjectObjectCursor<String, DiscoveryNode> node : nodes.getNodes()) {
202209
logger.debug("Sending remove ban for tasks with the parent [{}] to the node [{}]", request.parentTaskId, node.key);
203-
transportService.sendRequest(node.value, BAN_PARENT_ACTION_NAME, request, EmptyTransportResponseHandler
204-
.INSTANCE_SAME);
210+
transportService.sendRequest(node.value, BAN_PARENT_ACTION_NAME, request,
211+
new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {
212+
@Override
213+
public void handleException(TransportException exp) {
214+
assert ExceptionsHelper.unwrapCause(exp) instanceof ElasticsearchSecurityException == false;
215+
logger.info("failed to remove the parent ban for task {} on node {}", request.parentTaskId, node);
216+
}
217+
});
205218
}
206219
}
207220

0 commit comments

Comments
 (0)