From 40c827a3b80e0664a22f97184e24efdd2a33feaa Mon Sep 17 00:00:00 2001 From: David Roberts Date: Thu, 6 Jun 2019 11:00:34 +0100 Subject: [PATCH] [ML] Close sample stream in find_file_structure endpoint (#42896) A static code analysis revealed that we are not closing the input stream in the find_file_structure endpoint. This actually makes no difference in practice, as the particular InputStream implementation in this case is org.elasticsearch.common.bytes.BytesReferenceStreamInput and its close() method is a no-op. However, it is good practice to close the stream anyway. --- .../ml/action/TransportFindFileStructureAction.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFindFileStructureAction.java b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFindFileStructureAction.java index 3d4d78e7a95..63c292a9beb 100644 --- a/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFindFileStructureAction.java +++ b/x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/action/TransportFindFileStructureAction.java @@ -18,6 +18,8 @@ import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureFinder; import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureFinderManager; import org.elasticsearch.xpack.ml.filestructurefinder.FileStructureOverrides; +import java.io.InputStream; + public class TransportFindFileStructureAction extends HandledTransportAction { @@ -48,9 +50,11 @@ public class TransportFindFileStructureAction FileStructureFinderManager structureFinderManager = new FileStructureFinderManager(threadPool.scheduler()); - FileStructureFinder fileStructureFinder = structureFinderManager.findFileStructure(request.getLinesToSample(), - request.getLineMergeSizeLimit(), request.getSample().streamInput(), new FileStructureOverrides(request), request.getTimeout()); + try (InputStream sampleStream = request.getSample().streamInput()) { + FileStructureFinder fileStructureFinder = structureFinderManager.findFileStructure(request.getLinesToSample(), + request.getLineMergeSizeLimit(), sampleStream, new FileStructureOverrides(request), request.getTimeout()); - return new FindFileStructureAction.Response(fileStructureFinder.getStructure()); + return new FindFileStructureAction.Response(fileStructureFinder.getStructure()); + } } }