Skip to content

Commit 8e28cd1

Browse files
committed
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: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent 203e188 commit 8e28cd1

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/cmd/go/internal/modfetch/sumdb.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,15 @@ func (*dbClient) WriteConfig(file string, old, new []byte) error {
233233
// which will be deleted by "go clean -modcache".
234234
func (*dbClient) ReadCache(file string) ([]byte, error) {
235235
targ := filepath.Join(PkgMod, "download/cache/sumdb", file)
236-
return lockedfile.Read(targ)
236+
data, err := lockedfile.Read(targ)
237+
// lockedfile.Write does not atomically create the file with contents.
238+
// There is a moment between file creation and locking the file for writing,
239+
// during which the empty file can be locked for reading.
240+
// Treat observing an empty file as file not found.
241+
if err == nil && len(data) == 0 {
242+
err = &os.PathError{Op: "read", Path: targ, Err: os.ErrNotExist}
243+
}
244+
return data, err
237245
}
238246

239247
// WriteCache updates cached lookups or tiles.

0 commit comments

Comments
 (0)