diff --git a/core/src/main/java/hudson/model/Run.java b/core/src/main/java/hudson/model/Run.java
index 4701bfc396..b73330a1d0 100644
--- a/core/src/main/java/hudson/model/Run.java
+++ b/core/src/main/java/hudson/model/Run.java
@@ -41,6 +41,7 @@ import hudson.console.ConsoleLogFilter;
 import hudson.console.ConsoleNote;
 import hudson.console.ModelHyperlinkNote;
 import hudson.console.PlainTextConsoleOutputStream;
+import java.io.Closeable;
 import java.nio.file.Files;
 import java.nio.file.InvalidPathException;
 import java.nio.file.StandardOpenOption;
@@ -51,7 +52,6 @@ import hudson.cli.declarative.CLIMethod;
 import hudson.model.Descriptor.FormException;
 import hudson.model.listeners.RunListener;
 import hudson.model.listeners.SaveableListener;
-import hudson.model.queue.Executables;
 import hudson.model.queue.SubTask;
 import hudson.search.SearchIndexBuilder;
 import hudson.security.ACL;
@@ -1801,6 +1801,7 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
 		LOGGER.log(Level.SEVERE, "Failed to rotate log",e);
 	    }
         } finally {
+            IOUtils.closeQuietly(listener);
             onEndBuilding();
         }
     }
@@ -1815,23 +1816,33 @@ public abstract class Run <JobT extends Job<JobT,RunT>,RunT extends Run<JobT,Run
         } catch (InvalidPathException e) {
             throw new IOException(e);
         }
-        RunT build = job.getBuild();
+        Closeable closeable = logger;
+        try {
+            RunT build = job.getBuild();
 
-        // Global log filters
-        for (ConsoleLogFilter filter : ConsoleLogFilter.all()) {
-            logger = filter.decorateLogger(build, logger);
-        }
+            // Global log filters
+            for (ConsoleLogFilter filter : ConsoleLogFilter.all()) {
+                logger = filter.decorateLogger(build, logger);
+            }
+
+            // Project specific log filters
+            if (project instanceof BuildableItemWithBuildWrappers && build instanceof AbstractBuild) {
+                BuildableItemWithBuildWrappers biwbw = (BuildableItemWithBuildWrappers) project;
+                for (BuildWrapper bw : biwbw.getBuildWrappersList()) {
+                    logger = bw.decorateLogger((AbstractBuild) build, logger);
+                }
+            }
 
-        // Project specific log filters
-        if (project instanceof BuildableItemWithBuildWrappers && build instanceof AbstractBuild) {
-            BuildableItemWithBuildWrappers biwbw = (BuildableItemWithBuildWrappers) project;
-            for (BuildWrapper bw : biwbw.getBuildWrappersList()) {
-                logger = bw.decorateLogger((AbstractBuild) build, logger);
+            listener = new StreamBuildListener(logger, charset, closeable);
+            return listener;
+        } catch (IOException | InterruptedException e) {
+            try {
+                closeable.close();
+            } catch (IOException e1) {
+                e.addSuppressed(e1);
             }
+            throw e;
         }
-
-        listener = new StreamBuildListener(logger,charset);
-        return listener;
     }
 
     /**
diff --git a/core/src/main/java/hudson/model/StreamBuildListener.java b/core/src/main/java/hudson/model/StreamBuildListener.java
index aa6bff2521..ec61bd5bec 100644
--- a/core/src/main/java/hudson/model/StreamBuildListener.java
+++ b/core/src/main/java/hudson/model/StreamBuildListener.java
@@ -25,6 +25,7 @@ package hudson.model;
 
 import hudson.util.StreamTaskListener;
 
+import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -40,16 +41,26 @@ import java.util.List;
  * @author Kohsuke Kawaguchi
  */
 public class StreamBuildListener extends StreamTaskListener implements BuildListener {
+    private final Closeable[] additionalClosables;
+
+    public StreamBuildListener(OutputStream out, Charset charset, Closeable... additionalClosables) {
+        super(out, charset);
+        this.additionalClosables = additionalClosables == null ? null : additionalClosables.clone();
+    }
+
     public StreamBuildListener(OutputStream out, Charset charset) {
         super(out, charset);
+        additionalClosables = null;
     }
 
     public StreamBuildListener(File out, Charset charset) throws IOException {
         super(out, charset);
+        additionalClosables = null;
     }
 
     public StreamBuildListener(OutputStream w) {
         super(w);
+        additionalClosables = null;
     }
 
     /**
@@ -60,10 +71,12 @@ public class StreamBuildListener extends StreamTaskListener implements BuildList
     @Deprecated
     public StreamBuildListener(PrintStream w) {
         super(w);
+        additionalClosables = null;
     }
 
     public StreamBuildListener(PrintStream w, Charset charset) {
         super(w,charset);
+        additionalClosables = null;
     }
 
     public void started(List<Cause> causes) {
@@ -80,5 +93,41 @@ public class StreamBuildListener extends StreamTaskListener implements BuildList
         getLogger().println("Finished: "+result);
     }
 
+    @Override
+    public void close() throws IOException {
+        try {
+            super.close();
+        } catch (IOException e) {
+            if (additionalClosables != null) {
+                for (Closeable c: additionalClosables) {
+                    try {
+                        c.close();
+                    } catch (IOException e1) {
+                        e.addSuppressed(e1);
+                    }
+                }
+            }
+            throw e;
+        }
+        if (additionalClosables != null) {
+            IOException e = null;
+            for (Closeable c : additionalClosables) {
+                try {
+                    c.close();
+                } catch (IOException e1) {
+                    if (e == null) {
+                        e = e1;
+                    } else {
+                        e.addSuppressed(e1);
+                    }
+                }
+            }
+            if (e != null) {
+                throw e;
+            }
+        }
+
+    }
+
     private static final long serialVersionUID = 1L;
 }