diff --git a/docs/changelog/102715.yaml b/docs/changelog/102715.yaml new file mode 100644 index 0000000000000..7311db66ce151 --- /dev/null +++ b/docs/changelog/102715.yaml @@ -0,0 +1,6 @@ +pr: 102715 +summary: Fix leaking blocks in TopN +area: ES|QL +type: bug +issues: + - 102646 diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/ResultBuilderForDoc.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/ResultBuilderForDoc.java index 7fb507ffdbead..779e1dece2b33 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/ResultBuilderForDoc.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/ResultBuilderForDoc.java @@ -11,6 +11,8 @@ import org.elasticsearch.compute.data.Block; import org.elasticsearch.compute.data.BlockFactory; import org.elasticsearch.compute.data.DocVector; +import org.elasticsearch.compute.data.IntVector; +import org.elasticsearch.core.Releasables; class ResultBuilderForDoc implements ResultBuilder { private final BlockFactory blockFactory; @@ -42,12 +44,21 @@ public void decodeValue(BytesRef values) { @Override public Block build() { - return new DocVector( - blockFactory.newIntArrayVector(shards, position), - blockFactory.newIntArrayVector(segments, position), - blockFactory.newIntArrayVector(docs, position), - null - ).asBlock(); + boolean success = false; + IntVector shardsVector = null; + IntVector segmentsVector = null; + try { + shardsVector = blockFactory.newIntArrayVector(shards, position); + segmentsVector = blockFactory.newIntArrayVector(segments, position); + var docsVector = blockFactory.newIntArrayVector(docs, position); + var docsBlock = new DocVector(shardsVector, segmentsVector, docsVector, null).asBlock(); + success = true; + return docsBlock; + } finally { + if (success == false) { + Releasables.closeExpectNoException(shardsVector, segmentsVector); + } + } } @Override