internal/lsp/fake: retry ioutil.ReadFile on plan9 if it fails due to exclusive use

Fixes golang/go#50840.
(Maybe.)

Change-Id: I22d217f3706308b51d5b2ac84a781c50a6a41336
Reviewed-on: https://go-review.googlesource.com/c/tools/+/381734
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Bryan C. Mills 2022-01-28 10:26:33 -05:00 committed by Bryan Mills
parent 82366c6960
commit a739c97304
1 changed files with 15 additions and 4 deletions

View File

@ -12,6 +12,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
@ -152,11 +153,21 @@ func toURI(fp string) protocol.DocumentURI {
// ReadFile reads a text file specified by a workdir-relative path.
func (w *Workdir) ReadFile(path string) (string, error) {
b, err := ioutil.ReadFile(w.AbsPath(path))
if err != nil {
return "", err
backoff := 1 * time.Millisecond
for {
b, err := ioutil.ReadFile(w.AbsPath(path))
if err != nil {
if runtime.GOOS == "plan9" && strings.HasSuffix(err.Error(), " exclusive use file already open") {
// Plan 9 enforces exclusive access to locked files.
// Give the owner time to unlock it and retry.
time.Sleep(backoff)
backoff *= 2
continue
}
return "", err
}
return string(b), nil
}
return string(b), nil
}
func (w *Workdir) RegexpRange(path, re string) (Pos, Pos, error) {