os: fix Root.MkdirAll to handle race of directory creation

No tests were added, because in order to reproduce, the directory would
have to be created precisely between the rootOpenDir and mkdirat calls,
which is impossible to do in a test.

Fixes #75114

Change-Id: I6f86a5b33c87452c35728318eaf2169a7534ef37
Reviewed-on: https://go-review.googlesource.com/c/go/+/698215
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Sean Liao <sean@liao.dev>
This commit is contained in:
database64128 2025-08-22 01:03:42 +08:00 committed by Gopher Robot
parent 98238fd495
commit a076f49757

View file

@ -131,7 +131,9 @@ func rootMkdirAll(r *Root, fullname string, perm FileMode) error {
if try > 0 || !IsNotExist(err) {
return 0, &PathError{Op: "openat", Err: err}
}
if err := mkdirat(parent, name, perm); err != nil {
// Try again on EEXIST, because the directory may have been created
// by another process or thread between the rootOpenDir and mkdirat calls.
if err := mkdirat(parent, name, perm); err != nil && err != syscall.EEXIST {
return 0, &PathError{Op: "mkdirat", Err: err}
}
}