Skip to content
This repository has been archived by the owner on May 22, 2021. It is now read-only.

Commit

Permalink
Fix some temp dir leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilder committed Aug 14, 2014
1 parent 0d94a70 commit 64a065f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func debug(args ...interface{}) {
func fatalf(format string, args ...interface{}) {
if verbose {
fmt.Fprintf(os.Stderr, fmt.Sprintf("ERROR: %s", format), args...)
signals <- os.Interrupt
wg.Wait()
os.Exit(1)
}
}
Expand All @@ -30,6 +32,8 @@ func fatal(args ...interface{}) {
if verbose {
fmt.Fprint(os.Stderr, "ERROR: ")
fmt.Fprintln(os.Stderr, args...)
signals <- os.Interrupt
wg.Wait()
os.Exit(1)
}
}
33 changes: 27 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@ import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"sync"
"syscall"
)

var buildVersion string
var (
buildVersion string
signals chan os.Signal
wg sync.WaitGroup
)

func shutdown(tempdir string) {
defer wg.Done()
<-signals
debugf("Removing tempdir %s\n", tempdir)
err := os.RemoveAll(tempdir)
if err != nil {
fatal(err)
}

}

func main() {
var from, input, output, tempdir, tag string
Expand Down Expand Up @@ -50,11 +68,12 @@ func main() {
}
}

signals = make(chan os.Signal, 1)

if !keepTemp {
defer func() {
debugf("Removing tempdir %s\n", tempdir)
os.RemoveAll(tempdir)
}()
wg.Add(1)
signal.Notify(signals, os.Interrupt, os.Kill, syscall.SIGTERM)
go shutdown(tempdir)
}

export, err := LoadExport(input, tempdir)
Expand Down Expand Up @@ -175,7 +194,9 @@ func main() {
}

debug("Done. New image created.")

signals <- os.Interrupt
wg.Wait()
// print our new history
export.PrintHistory()

}

0 comments on commit 64a065f

Please sign in to comment.