@@ -471,7 +471,14 @@ public void executeQueryPhase(InternalScrollSearchRequest request,
471
471
SearchShardTask task ,
472
472
ActionListener <ScrollQuerySearchResult > listener ) {
473
473
final LegacyReaderContext readerContext = (LegacyReaderContext ) findReaderContext (request .contextId (), request );
474
- final Releasable markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
474
+ final Releasable markAsUsed ;
475
+ try {
476
+ markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
477
+ } catch (Exception e ) {
478
+ // We need to release the reader context of the scroll when we hit any exception (here the keep_alive can be too large)
479
+ freeReaderContext (readerContext .id ());
480
+ throw e ;
481
+ }
475
482
runAsync (getExecutor (readerContext .indexShard ()), () -> {
476
483
final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (null );
477
484
try (SearchContext searchContext = createContext (readerContext , shardSearchRequest , task , false );
@@ -536,7 +543,14 @@ private Executor getExecutor(IndexShard indexShard) {
536
543
public void executeFetchPhase (InternalScrollSearchRequest request , SearchShardTask task ,
537
544
ActionListener <ScrollQueryFetchSearchResult > listener ) {
538
545
final LegacyReaderContext readerContext = (LegacyReaderContext ) findReaderContext (request .contextId (), request );
539
- final Releasable markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
546
+ final Releasable markAsUsed ;
547
+ try {
548
+ markAsUsed = readerContext .markAsUsed (getScrollKeepAlive (request .scroll ()));
549
+ } catch (Exception e ) {
550
+ // We need to release the reader context of the scroll when we hit any exception (here the keep_alive can be too large)
551
+ freeReaderContext (readerContext .id ());
552
+ throw e ;
553
+ }
540
554
runAsync (getExecutor (readerContext .indexShard ()), () -> {
541
555
final ShardSearchRequest shardSearchRequest = readerContext .getShardSearchRequest (null );
542
556
try (SearchContext searchContext = createContext (readerContext , shardSearchRequest , task , false );
0 commit comments