]> Cypherpunks repositories - gostls13.git/commitdiff
os: fix Root.MkdirAll to handle race of directory creation
authordatabase64128 <free122448@hotmail.com>
Thu, 21 Aug 2025 17:03:42 +0000 (01:03 +0800)
committerGopher Robot <gobot@golang.org>
Fri, 22 Aug 2025 18:21:34 +0000 (11:21 -0700)
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>

src/os/root_openat.go

index e433bd5093fdb014f42a4fb487ac58019fa0b20a..83bde5ef14160d08763b017283351251e355b5f1 100644 (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}
                        }
                }