diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java index 8bbe17d986..1252725f6d 100644 --- a/core/src/main/java/hudson/model/Run.java +++ b/core/src/main/java/hudson/model/Run.java @@ -924,15 +924,19 @@ public abstract class Run ,RunT extends Run logLines = new LinkedList(); BufferedReader reader = new BufferedReader(new FileReader(getLogFile())); - for (String line = reader.readLine(); line != null; line = reader.readLine()) { - logLines.add(line); - ++lineCount; - // If we have too many lines, remove the oldest line. This way we - // never have to hold the full contents of a huge log file in memory. - // Adding to and removing from the ends of a linked list are cheap - // operations. - if (lineCount > maxLines) - logLines.remove(0); + try { + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + logLines.add(line); + ++lineCount; + // If we have too many lines, remove the oldest line. This way we + // never have to hold the full contents of a huge log file in memory. + // Adding to and removing from the ends of a linked list are cheap + // operations. + if (lineCount > maxLines) + logLines.remove(0); + } + } finally { + reader.close(); } // If the log has been truncated, include that information.