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

Commit

Permalink
cmd/pprof: remove tempDir when no longer needed
Browse files Browse the repository at this point in the history
The pprof tools properly cleans up all files it creates, but forgets
to clean up the temporary directory itself. This CL fixes that.

Fixes #13863

Change-Id: I1151c36cdad5ace7cc97e7e04001cf0149ef0f63
Reviewed-on: https://go-review.googlesource.com/23019
Reviewed-by: Brad Fitzpatrick <[email protected]>
Run-TryBot: Joe Tsai <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
dsnet committed May 11, 2016
1 parent 2ffb3e5 commit e9407ae
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/cmd/internal/pprof/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func makeVizTmpDir() error {
if err != nil {
return err
}
tempfile.DeferDelete(name)
vizTmpDir = name
return nil
}
Expand Down
9 changes: 5 additions & 4 deletions src/cmd/internal/pprof/tempfile/tempfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ func New(dir, prefix, suffix string) (*os.File, error) {
var tempFiles []string
var tempFilesMu = sync.Mutex{}

// DeferDelete marks a file to be deleted by next call to Cleanup()
// DeferDelete marks a file or directory to be deleted by next call to Cleanup.
func DeferDelete(path string) {
tempFilesMu.Lock()
tempFiles = append(tempFiles, path)
tempFilesMu.Unlock()
}

// Cleanup removes any temporary files selected for deferred cleaning.
// Cleanup removes any temporary files or directories selected for deferred cleaning.
// Similar to defer semantics, the nodes are deleted in LIFO order.
func Cleanup() {
tempFilesMu.Lock()
for _, f := range tempFiles {
os.Remove(f)
for i := len(tempFiles) - 1; i >= 0; i-- {
os.Remove(tempFiles[i])
}
tempFiles = nil
tempFilesMu.Unlock()
Expand Down

0 comments on commit e9407ae

Please sign in to comment.