Perhaps in this run the search was cancelled too early? Doesn't reproduce for me anyway so seems like some kind of race.

**Build scan:**
https://gradle-enterprise.elastic.co/s/gxwsmk4hxf5y2/tests/:server:internalClusterTest/org.elasticsearch.search.SearchCancellationIT/testCancellationDuringTimeSeriesAggregation

**Reproduction line:**
`./gradlew ':server:internalClusterTest' --tests "org.elasticsearch.search.SearchCancellationIT.testCancellationDuringTimeSeriesAggregation" -Dtests.seed=B858CAC59575905 -Dtests.locale=sr-RS -Dtests.timezone=America/Catamarca -Druntime.java=17`

**Applicable branches:**
master

**Reproduces locally?:**
No

**Failure history:**
https://gradle-enterprise.elastic.co/scans/tests?tests.container=org.elasticsearch.search.SearchCancellationIT&tests.test=testCancellationDuringTimeSeriesAggregation

**Failure excerpt:**
```
java.lang.AssertionError: 
Expected: a string containing "TimeSeriesIndexSearcher"
     but: was "Failed to execute phase [fetch], ; shardFailures {[5R6jMFYWQhyExkaRiv8xpA][test][0]: org.elasticsearch.transport.RemoteTransportException: [node_s0][127.0.0.1:36497][indices:data/read/search[phase/query]]
Caused by: org.elasticsearch.search.query.QueryPhaseExecutionException: Query Failed [Failed to execute main query]
	at org.elasticsearch.search.query.QueryPhase.executeInternal(QueryPhase.java:205)
	at org.elasticsearch.search.query.QueryPhase.execute(QueryPhase.java:88)
	at org.elasticsearch.search.SearchService.loadOrExecuteQueryPhase(SearchService.java:462)
	at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:625)
	at org.elasticsearch.search.SearchService.lambda$executeQueryPhase$2(SearchService.java:487)
	at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:47)
	at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:62)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:33)
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:776)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: org.elasticsearch.tasks.TaskCancelledException: task cancelled [by user request]
	at org.elasticsearch.tasks.CancellableTask.getTaskCancelledException(CancellableTask.java:121)
	at org.elasticsearch.tasks.CancellableTask.ensureNotCancelled(CancellableTask.java:97)
	at org.elasticsearch.search.DefaultSearchContext.lambda$preProcess$0(DefaultSearchContext.java:269)
	at org.elasticsearch.search.internal.ContextIndexSearcher$MutableQueryTimeout.checkCancelled(ContextIndexSearcher.java:337)
	at org.elasticsearch.search.internal.ContextIndexSearcher.searchLeaf(ContextIndexSearcher.java:178)
	at org.elasticsearch.search.internal.ContextIndexSearcher.search(ContextIndexSearcher.java:167)
	at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:541)
	at org.elasticsearch.search.query.QueryPhase.searchWithCollector(QueryPhase.java:232)
	at org.elasticsearch.search.query.QueryPhase.executeInternal(QueryPhase.java:187)
	... 13 more
}{[5R6jMFYWQhyExkaRiv8xpA][test][2]: org.elasticsearch.transport.RemoteTransportException: [node_s0][127.0.0.1:36497][indices:data/read/search[phase/query]]
Caused by: org.elasticsearch.tasks.TaskCancelledException: task cancelled [by user request]
	at org.elasticsearch.tasks.CancellableTask.getTaskCancelledException(CancellableTask.java:121)
	at org.elasticsearch.tasks.CancellableTask.ensureNotCancelled(CancellableTask.java:97)
	at org.elasticsearch.search.SearchService.checkCancelled(SearchService.java:814)
	at org.elasticsearch.search.SearchService.createContext(SearchService.java:965)
	at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:621)
	at org.elasticsearch.search.SearchService.lambda$executeQueryPhase$2(SearchService.java:487)
	at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:47)
	at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:62)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:33)
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:776)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
}{[5R6jMFYWQhyExkaRiv8xpA][test][3]: org.elasticsearch.transport.RemoteTransportException: [node_s0][127.0.0.1:36497][indices:data/read/search[phase/query]]
Caused by: org.elasticsearch.tasks.TaskCancelledException: task cancelled [by user request]
	at org.elasticsearch.tasks.CancellableTask.getTaskCancelledException(CancellableTask.java:121)
	at org.elasticsearch.tasks.CancellableTask.ensureNotCancelled(CancellableTask.java:97)
	at org.elasticsearch.search.SearchService.checkCancelled(SearchService.java:814)
	at org.elasticsearch.search.SearchService.createContext(SearchService.java:965)
	at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:621)
	at org.elasticsearch.search.SearchService.lambda$executeQueryPhase$2(SearchService.java:487)
	at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:47)
	at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:62)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:33)
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:776)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
}
	at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:716)
	at org.elasticsearch.action.search.FetchSearchPhase$1.onFailure(FetchSearchPhase.java:103)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:28)
	at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:33)
	at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:776)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: org.elasticsearch.tasks.TaskCancelledException: Cancelled
	at org.elasticsearch.search.aggregations.AggregationReduceContext.consumeBucketsAndMaybeBreak(AggregationReduceContext.java:117)
	at org.elasticsearch.search.aggregations.timeseries.InternalTimeSeries.reduce(InternalTimeSeries.java:217)
	at org.elasticsearch.search.aggregations.InternalAggregations.reduce(InternalAggregations.java:155)
	at org.elasticsearch.search.aggregations.InternalAggregations.topLevelReduce(InternalAggregations.java:102)
	at org.elasticsearch.action.search.SearchPhaseController.reduceAggs(SearchPhaseController.java:532)
	at org.elasticsearch.action.search.SearchPhaseController.reducedQueryPhase(SearchPhaseController.java:501)
	at org.elasticsearch.action.search.QueryPhaseResultConsumer.reduce(QueryPhaseResultConsumer.java:132)
	at org.elasticsearch.action.search.FetchSearchPhase.innerRun(FetchSearchPhase.java:112)
	at org.elasticsearch.action.search.FetchSearchPhase$1.doRun(FetchSearchPhase.java:98)
	at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)
	... 6 more
"

  at __randomizedtesting.SeedInfo.seed([B858CAC59575905:89ED0086CDB94051]:0)
  at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
  at org.junit.Assert.assertThat(Assert.java:956)
  at org.junit.Assert.assertThat(Assert.java:923)
  at org.elasticsearch.search.SearchCancellationIT.testCancellationDuringTimeSeriesAggregation(SearchCancellationIT.java:330)
  at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-2)
  at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
  at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:568)
  at com.carrotsearch.randomizedtesting.RandomizedRunner.invoke(RandomizedRunner.java:1758)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$8.evaluate(RandomizedRunner.java:946)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$9.evaluate(RandomizedRunner.java:982)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$10.evaluate(RandomizedRunner.java:996)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at org.apache.lucene.util.TestRuleSetupTeardownChained$1.evaluate(TestRuleSetupTeardownChained.java:44)
  at org.apache.lucene.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
  at org.apache.lucene.util.TestRuleThreadAndTestName$1.evaluate(TestRuleThreadAndTestName.java:45)
  at org.apache.lucene.util.TestRuleIgnoreAfterMaxFailures$1.evaluate(TestRuleIgnoreAfterMaxFailures.java:60)
  at org.apache.lucene.util.TestRuleMarkFailure$1.evaluate(TestRuleMarkFailure.java:44)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl$StatementRunner.run(ThreadLeakControl.java:375)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl.forkTimeoutingTask(ThreadLeakControl.java:824)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl$3.evaluate(ThreadLeakControl.java:475)
  at com.carrotsearch.randomizedtesting.RandomizedRunner.runSingleTest(RandomizedRunner.java:955)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$5.evaluate(RandomizedRunner.java:840)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$6.evaluate(RandomizedRunner.java:891)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$7.evaluate(RandomizedRunner.java:902)
  at org.apache.lucene.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at org.apache.lucene.util.TestRuleStoreClassName$1.evaluate(TestRuleStoreClassName.java:38)
  at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
  at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at org.apache.lucene.util.TestRuleAssertionsRequired$1.evaluate(TestRuleAssertionsRequired.java:53)
  at org.apache.lucene.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
  at org.apache.lucene.util.TestRuleMarkFailure$1.evaluate(TestRuleMarkFailure.java:44)
  at org.apache.lucene.util.TestRuleIgnoreAfterMaxFailures$1.evaluate(TestRuleIgnoreAfterMaxFailures.java:60)
  at org.apache.lucene.util.TestRuleIgnoreTestSuites$1.evaluate(TestRuleIgnoreTestSuites.java:47)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl$StatementRunner.run(ThreadLeakControl.java:375)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl.lambda$forkTimeoutingTask$0(ThreadLeakControl.java:831)
  at java.lang.Thread.run(Thread.java:833)

```
server/src/internalClusterTest/java/org/elasticsearch/search/SearchCancellationIT.java
import org.elasticsearch.tasks.TaskInfo;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.transport.TransportService;
import org.junit.BeforeClass;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static org.elasticsearch.index.IndexSettings.TIME_SERIES_END_TIME;
import static org.elasticsearch.index.IndexSettings.TIME_SERIES_START_TIME;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.scriptQuery;
import static org.elasticsearch.search.SearchCancellationIT.ScriptedBlockPlugin.SEARCH_BLOCK_SCRIPT_NAME;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFailures;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.notNullValue;
@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE)