From 1e1f6191439cf3ad32f3ba54bba5a0185dd55b14 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Sat, 1 Feb 2025 09:58:19 +0330 Subject: [PATCH] os: fix the race condition on dir_unix Signed-off-by: Amirhossein Akhlaghpour --- src/os/dir_unix.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/os/dir_unix.go b/src/os/dir_unix.go index eadc1660c2..6a0135b70b 100644 --- a/src/os/dir_unix.go +++ b/src/os/dir_unix.go @@ -46,11 +46,19 @@ func (d *dirInfo) close() { func (f *File) readdir(n int, mode readdirMode) (names []string, dirents []DirEntry, infos []FileInfo, err error) { // If this file has no dirInfo, create one. - d := f.dirinfo.Load() - if d == nil { - d = new(dirInfo) - f.dirinfo.Store(d) + var d *dirInfo + for { + d = f.dirinfo.Load() + if d != nil { + break + } + newD := new(dirInfo) + if f.dirinfo.CompareAndSwap(nil, newD) { + d = newD + break + } } + d.mu.Lock() defer d.mu.Unlock() if d.buf == nil {