From ae95caa60e094b91396f16a53655551d72c79491 Mon Sep 17 00:00:00 2001 From: lujiefsi Date: Mon, 18 Oct 2021 14:24:15 +0800 Subject: [PATCH] YARN-10976. Fix resource leak due to Files.walk (#3552) Signed-off-by: Akira Ajisaka --- .../resourceplugin/com/nec/VEDeviceDiscoverer.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/resourceplugin/com/nec/VEDeviceDiscoverer.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/resourceplugin/com/nec/VEDeviceDiscoverer.java index 4bc92f27117..8d801198644 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/resourceplugin/com/nec/VEDeviceDiscoverer.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/resourceplugin/com/nec/VEDeviceDiscoverer.java @@ -27,6 +27,7 @@ import java.nio.file.Paths; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.lang3.mutable.MutableInt; import org.apache.hadoop.util.Shell; @@ -58,11 +59,11 @@ class VEDeviceDiscoverer { public Set getDevicesFromPath(String path) throws IOException { MutableInt counter = new MutableInt(0); - - return Files.walk(Paths.get(path), 1) - .filter(p -> p.toFile().getName().startsWith("veslot")) - .map(p -> toDevice(p, counter)) - .collect(Collectors.toSet()); + try (Stream stream = Files.walk(Paths.get(path), 1)) { + return stream.filter(p -> p.toFile().getName().startsWith("veslot")) + .map(p -> toDevice(p, counter)) + .collect(Collectors.toSet()); + } } private Device toDevice(Path p, MutableInt counter) { @@ -140,4 +141,4 @@ class VEDeviceDiscoverer { Function provider) { this.commandExecutorProvider = provider; } -} \ No newline at end of file +}