Instead we should use `org.elasticsearch.tasks.CancellableTask#addListener` to add a listener which sets `failure` on cancellation.
x-pack/plugin/snapshot-repo-test-kit/src/main/java/org/elasticsearch/repositories/blobstore/testkit/RepositoryAnalyzeAction.java
private final RepositoryPerformanceSummary.Builder summary = new RepositoryPerformanceSummary.Builder();
        public AsyncAction(
            TransportService transportService,
            BlobStoreRepository repository,
            CancellableTask task,
            Request request,
            DiscoveryNodes discoveryNodes,
            TransportVersion minClusterTransportVersion,
            LongSupplier currentTimeMillisSupplier,
            ActionListener<Response> listener
        ) {
            this.transportService = transportService;
            this.repository = repository;
            this.task = task;
            this.request = request;
            this.discoveryNodes = discoveryNodes;
            this.minClusterTransportVersion = minClusterTransportVersion;
            this.currentTimeMillisSupplier = currentTimeMillisSupplier;
            this.timeoutTimeMillis = currentTimeMillisSupplier.getAsLong() + request.getTimeout().millis();
            this.cancellationListener = new SubscribableListener<>();
            this.listener = ActionListener.runBefore(listener, () -> cancellationListener.onResponse(null));
            responses = new ArrayList<>(request.blobCount);
        }
        private boolean setFirstFailure(Exception e) {
            if (failure.compareAndSet(null, e)) {
                transportService.getTaskManager().cancelTaskAndDescendants(task, "task failed", false, ActionListener.noop());
                return true;
            } else {
                return false;
            }