]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/renameio: mask spurious "Access is denied" errors on Windows
authorBryan C. Mills <bcmills@google.com>
Tue, 16 Apr 2019 20:14:31 +0000 (16:14 -0400)
committerBryan C. Mills <bcmills@google.com>
Tue, 16 Apr 2019 20:24:45 +0000 (20:24 +0000)
Fixes #31247

Change-Id: I85a760a5d36ae835c97a13f980804d06b658857e
Reviewed-on: https://go-review.googlesource.com/c/go/+/172418
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/go/internal/renameio/renameio.go

index 8f59e1a57732579bfc547e49b227955f300cdba5..3f3f1708fa48b27d7e1b1ed867d7ce2ae7c4d220 100644 (file)
@@ -11,6 +11,9 @@ import (
        "io/ioutil"
        "os"
        "path/filepath"
+       "runtime"
+       "strings"
+       "time"
 )
 
 const patternSuffix = "*.tmp"
@@ -59,5 +62,22 @@ func WriteToFile(filename string, data io.Reader) (err error) {
        if err := f.Close(); err != nil {
                return err
        }
-       return os.Rename(f.Name(), filename)
+
+       var start time.Time
+       for {
+               err := os.Rename(f.Name(), filename)
+               if err == nil || runtime.GOOS != "windows" || !strings.HasSuffix(err.Error(), "Access is denied.") {
+                       return err
+               }
+
+               // Windows seems to occasionally trigger spurious "Access is denied" errors
+               // here (see golang.org/issue/31247). We're not sure why. It's probably
+               // worth a little extra latency to avoid propagating the spurious errors.
+               if start.IsZero() {
+                       start = time.Now()
+               } else if time.Since(start) >= 500*time.Millisecond {
+                       return err
+               }
+               time.Sleep(5 * time.Millisecond)
+       }
 }