Today the `ReservedClusterStateService` decides whether or not to submit an update-error task depending on the content of the last-applied state as exposed by `ClusterService#state()`. This may happen concurrently with another state being published, so does not give a fully consistent view of the latest state. Instead it should check the state within the cluster state task executor, triggering a no-op update as appropriate.
server/src/main/java/org/elasticsearch/reservedstate/service/ReservedStateErrorTaskExecutor.java
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.ClusterStateTaskExecutor;
/**
 * Reserved cluster error state task executor
 * <p>
 * We use this task executor to record any errors while updating and reserving the cluster state
 */
record ReservedStateErrorTaskExecutor() implements ClusterStateTaskExecutor<ReservedStateErrorTask> {
    private static final Logger logger = LogManager.getLogger(ReservedStateErrorTaskExecutor.class);
    @Override
    public ClusterState execute(BatchExecutionContext<ReservedStateErrorTask> batchExecutionContext) {
        var updatedState = batchExecutionContext.initialState();
        for (final var taskContext : batchExecutionContext.taskContexts()) {
            final var task = taskContext.getTask();
            try (var ignored = taskContext.captureResponseHeaders()) {
                updatedState = task.execute(updatedState);
            }
            taskContext.success(() -> task.listener().onResponse(ActionResponse.Empty.INSTANCE));
        }
        return updatedState;
    }
    @Override
    public void clusterStatePublished(ClusterState newClusterState) {
        logger.debug("Wrote new error state in reserved cluster state metadata");
    }
}