diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java index 55be001443b..11067b6ee3c 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/ProcfsBasedProcessTree.java @@ -518,22 +518,15 @@ private static ProcessInfo constructProcessInfo(ProcessInfo pinfo, String procfsDir) { ProcessInfo ret = null; // Read "procfsDir//stat" file - typically /proc//stat - BufferedReader in = null; - InputStreamReader fReader = null; - try { - File pidDir = new File(procfsDir, pinfo.getPid()); - fReader = new InputStreamReader( - new FileInputStream( - new File(pidDir, PROCFS_STAT_FILE)), Charset.forName("UTF-8")); - in = new BufferedReader(fReader); - } catch (FileNotFoundException f) { - // The process vanished in the interim! + final File pidDir = new File(procfsDir, pinfo.getPid()); + final File procFsStatFile = new File(pidDir, PROCFS_STAT_FILE); + if (!procFsStatFile.exists()) { return ret; } ret = pinfo; try { - String str = in.readLine(); // only one line + String str = readFirstLineFromFile(procFsStatFile); Matcher m = PROCFS_STAT_FILE_FORMAT.matcher(str); boolean mat = m.find(); if (mat) { @@ -551,22 +544,20 @@ private static ProcessInfo constructProcessInfo(ProcessInfo pinfo, } catch (IOException io) { LOG.warn("Error reading the stream", io); ret = null; - } finally { - // Close the streams - try { - fReader.close(); - try { - in.close(); - } catch (IOException i) { - LOG.warn("Error closing the stream", i); - } - } catch (IOException i) { - LOG.warn("Error closing the stream", i); - } } return ret; } + + private static String readFirstLineFromFile(File file) throws IOException { + try (final InputStreamReader fReader = new InputStreamReader( + new FileInputStream(file, Charset.forName("UTF-8")))) { + try (final BufferedReader in = new BufferedReader(fReader)) { + return in.readLine(); + } + } + } + /** * Returns a string printing PIDs of process present in the * ProcfsBasedProcessTree. Output format : [pid pid ..]