From 0aee096fd73676e2548e3bf132770f1692ac47dd Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 17 Oct 2013 14:46:58 +0200 Subject: [PATCH] TarFilter: Fix leak of tmpfiles We were leaking the temporary directory that we create in TarFilter, because the "tmpDir, err := ioutil.TempDir()" call overrides the tmpDir in the outer scope with a new locally scoped variable. This means tmpDir is always "" when the cleanup function is called. Also, we did not call the atExit() function if CmdStream had an error early on. On errors in CmdStream(), --- archive.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/archive.go b/archive.go index 75b6e7e1f1..6ea436c9bb 100644 --- a/archive.go +++ b/archive.go @@ -120,7 +120,8 @@ func TarFilter(path string, compression Compression, filter []string, recursive tmpDir := "" if createFiles != nil { - tmpDir, err := ioutil.TempDir("", "docker-tar") + var err error // Can't use := here or we override the outer tmpDir + tmpDir, err = ioutil.TempDir("", "docker-tar") if err != nil { return nil, err } @@ -284,6 +285,9 @@ func CmdStream(cmd *exec.Cmd, input *string, atEnd func()) (io.Reader, error) { if input != nil { stdin, err := cmd.StdinPipe() if err != nil { + if atEnd != nil { + atEnd() + } return nil, err } // Write stdin if any @@ -294,10 +298,16 @@ func CmdStream(cmd *exec.Cmd, input *string, atEnd func()) (io.Reader, error) { } stdout, err := cmd.StdoutPipe() if err != nil { + if atEnd != nil { + atEnd() + } return nil, err } stderr, err := cmd.StderrPipe() if err != nil { + if atEnd != nil { + atEnd() + } return nil, err } pipeR, pipeW := io.Pipe()