Skip to content

Commit 06056fc

Browse files
authored
Remove ban tasks with the current thread context (#55404)
If a task runs with a user, and it's canceled after we have sent the 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. Backport of #55404
1 parent 1b0d246 commit 06056fc

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;
@@ -109,7 +111,11 @@ protected synchronized void taskOperation(CancelTasksRequest request, Cancellabl
109111
final boolean canceled;
110112
if (cancellableTask.shouldCancelChildrenOnCancellation()) {
111113
DiscoveryNodes childNodes = clusterService.state().nodes();
112-
final BanLock banLock = new BanLock(childNodes.getSize(), () -> removeBanOnNodes(cancellableTask, childNodes));
114+
// If the task runs with a user, and it's cancelled after we have sent ban requests, then the unban request
115+
// will be denied as it must not execute with a user. We need to wrap it with the current thread context.
116+
final Runnable removeBans = transportService.getThreadPool().getThreadContext()
117+
.preserveContext(() -> removeBanOnNodes(cancellableTask, childNodes));
118+
final BanLock banLock = new BanLock(childNodes.getSize(), removeBans);
113119
canceled = taskManager.cancel(cancellableTask, request.getReason(), banLock::onTaskFinished);
114120
if (canceled) {
115121
// /In case the task has some child tasks, we need to wait for until ban is set on all nodes
@@ -182,6 +188,7 @@ public void handleResponse(TransportResponse.Empty response) {
182188

183189
@Override
184190
public void handleException(TransportException exp) {
191+
assert ExceptionsHelper.unwrapCause(exp) instanceof ElasticsearchSecurityException == false;
185192
logger.warn("Cannot send ban for tasks with the parent [{}] to the node [{}]", request.parentTaskId, node.key);
186193
listener.onFailure(exp);
187194
}
@@ -192,8 +199,14 @@ public void handleException(TransportException exp) {
192199
private void sendRemoveBanRequest(DiscoveryNodes nodes, BanParentTaskRequest request) {
193200
for (ObjectObjectCursor<String, DiscoveryNode> node : nodes.getNodes()) {
194201
logger.debug("Sending remove ban for tasks with the parent [{}] to the node [{}]", request.parentTaskId, node.key);
195-
transportService.sendRequest(node.value, BAN_PARENT_ACTION_NAME, request, EmptyTransportResponseHandler
196-
.INSTANCE_SAME);
202+
transportService.sendRequest(node.value, BAN_PARENT_ACTION_NAME, request,
203+
new EmptyTransportResponseHandler(ThreadPool.Names.SAME) {
204+
@Override
205+
public void handleException(TransportException exp) {
206+
assert ExceptionsHelper.unwrapCause(exp) instanceof ElasticsearchSecurityException == false;
207+
logger.info("failed to remove the parent ban for task {} on node {}", request.parentTaskId, node);
208+
}
209+
});
197210
}
198211
}
199212

0 commit comments

Comments
 (0)