os: fix the race condition on dir_unix

Signed-off-by: Amirhossein Akhlaghpour <m9.akhlaghpoor@gmail.com>
This commit is contained in:
Amirhossein Akhlaghpour 2025-02-01 09:58:19 +03:30
parent eac6fc15cc
commit 1e1f619143
No known key found for this signature in database
GPG Key ID: D7A35F11FB70A961
1 changed files with 12 additions and 4 deletions

View File

@ -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 {