Commit 8e28cd19 authored by Russ Cox's avatar Russ Cox

cmd/go/internal/modfetch: fix concurrent read/write race in modfetch

On Windows systems, the failure rate for cmd/go's TestScript/mod_concurrent
is somewhere around 3-10% without this change. With the change, I have yet
to see a failure.

Fixes #31744.

Change-Id: Ib321ebb9556dd8438086cf329dfa083a9e051732
Reviewed-on: https://go-review.googlesource.com/c/go/+/174439
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 203e1888
...@@ -233,7 +233,15 @@ func (*dbClient) WriteConfig(file string, old, new []byte) error { ...@@ -233,7 +233,15 @@ func (*dbClient) WriteConfig(file string, old, new []byte) error {
// which will be deleted by "go clean -modcache". // which will be deleted by "go clean -modcache".
func (*dbClient) ReadCache(file string) ([]byte, error) { func (*dbClient) ReadCache(file string) ([]byte, error) {
targ := filepath.Join(PkgMod, "download/cache/sumdb", file) targ := filepath.Join(PkgMod, "download/cache/sumdb", file)
return lockedfile.Read(targ) data, err := lockedfile.Read(targ)
// lockedfile.Write does not atomically create the file with contents.
// There is a moment between file creation and locking the file for writing,
// during which the empty file can be locked for reading.
// Treat observing an empty file as file not found.
if err == nil && len(data) == 0 {
err = &os.PathError{Op: "read", Path: targ, Err: os.ErrNotExist}
}
return data, err
} }
// WriteCache updates cached lookups or tiles. // WriteCache updates cached lookups or tiles.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment