Skip to content

Commit 56b8ee2

Browse files
author
Bryan C. Mills
committed
cmd/go: retry RemoveAll(workdir) for up to 500ms
On some configurations of Windows, directories containing executable files may be locked for a while after the executable exits (perhaps due to antivirus scans?). It's probably worth a little extra latency on exit to avoid filling up the user's temporary directory with leaked files. Updates #30789 Change-Id: Iae7fcdd07fb9ecfb05967cfe0c8833db646d2f85 Reviewed-on: https://go-review.googlesource.com/c/go/+/172337 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 518ee55 commit 56b8ee2

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Diff for: src/cmd/go/internal/work/action.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import (
1616
"io/ioutil"
1717
"os"
1818
"path/filepath"
19+
"runtime"
1920
"strings"
2021
"sync"
22+
"time"
2123

2224
"cmd/go/internal/base"
2325
"cmd/go/internal/cache"
@@ -243,8 +245,23 @@ func (b *Builder) Init() {
243245
if !cfg.BuildWork {
244246
workdir := b.WorkDir
245247
base.AtExit(func() {
246-
if err := os.RemoveAll(workdir); err != nil {
247-
fmt.Fprintf(os.Stderr, "go: failed to remove work dir: %s\n", err)
248+
start := time.Now()
249+
for {
250+
err := os.RemoveAll(workdir)
251+
if err == nil {
252+
return
253+
}
254+
255+
// On some configurations of Windows, directories containing executable
256+
// files may be locked for a while after the executable exits (perhaps
257+
// due to antivirus scans?). It's probably worth a little extra latency
258+
// on exit to avoid filling up the user's temporary directory with leaked
259+
// files. (See golang.org/issue/30789.)
260+
if runtime.GOOS != "windows" || time.Since(start) >= 500*time.Millisecond {
261+
fmt.Fprintf(os.Stderr, "go: failed to remove work dir: %s\n", err)
262+
return
263+
}
264+
time.Sleep(5 * time.Millisecond)
248265
}
249266
})
250267
}

0 commit comments

Comments
 (0)