From 9d8d6ff5862c8f13117132451c08dffb27f97804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 14 Nov 2022 18:11:51 +0100 Subject: [PATCH] Fix concurrency issue in MemMapFs.Mkdir/MkdirAll * The backing map is protected by a RWMutex * This commit double checks for the existence of the directory inside the write lock to avoid potential data races when multiple goroutines tries to create the same directory. Fixes #361 Fixes #298 --- memmap.go | 5 ++++ memmap_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/memmap.go b/memmap.go index ea0798d8..d06975e7 100644 --- a/memmap.go +++ b/memmap.go @@ -142,6 +142,11 @@ func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error { } m.mu.Lock() + // Dobule check that it doesn't exist. + if _, ok := m.getData()[name]; ok { + m.mu.Unlock() + return &os.PathError{Op: "mkdir", Path: name, Err: ErrFileExists} + } item := mem.CreateDir(name) mem.SetMode(item, os.ModeDir|perm) m.getData()[name] = item diff --git a/memmap_test.go b/memmap_test.go index 94f70b23..d48d0129 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -3,11 +3,16 @@ package afero import ( "fmt" "io" + "io/fs" "os" "path/filepath" "runtime" + "strings" + "sync" "testing" "time" + + "github.com/stretchr/testify/require" ) func TestNormalizePath(t *testing.T) { @@ -692,3 +697,75 @@ func TestMemFsLstatIfPossible(t *testing.T) { t.Fatalf("Function indicated lstat was called. This should never be true.") } } + +func TestListFilesAfterConcurrencyFileWrite(t *testing.T) { + const dir = "test_dir" + const n = 1000 + mfs := NewMemMapFs().(*MemMapFs) + assert := require.New(t) + + allFilePaths := make([]string, 0, n) + + // run concurrency test + var wg sync.WaitGroup + for i := 0; i < n; i++ { + fp := filepath.Join( + dir, + fmt.Sprintf("%02d", n%10), + fmt.Sprintf("%d.txt", i), + ) + allFilePaths = append(allFilePaths, fp) + + wg.Add(1) + go func() { + defer wg.Done() + + if err := mfs.MkdirAll(filepath.Dir(fp), 0755); err != nil { + assert.NoError(err) + } + + wt, err := mfs.Create(fp) + if err != nil { + assert.NoError(err) + } + assert.NoError(err) + defer func() { + assert.NoError(wt.Close()) + }() + + // write 30 bytes + for j := 0; j < 10; j++ { + _, err := wt.Write([]byte("000")) + assert.NoError(err) + } + }() + } + wg.Wait() + + // Test1: find all files by full path access + seen := make(map[string]bool) + for _, fp := range allFilePaths { + if seen[fp] { + fmt.Println("duplicate file path:", fp) + } + info, err := mfs.Stat(fp) + assert.NoErrorf(err, "stat file %s", fp) + assert.Equalf(int64(30), info.Size(), "file %s size unmatch", fp) + seen[fp] = true + } + + // Test2: find all files by walk + foundFiles := make([]string, 0, n) + wErr := Walk(mfs, dir, func(path string, info fs.FileInfo, err error) error { + assert.NoError(err) + if info.IsDir() { + return nil // skip dir + } + if strings.HasSuffix(info.Name(), ".txt") { + foundFiles = append(foundFiles, path) + } + return nil + }) + assert.NoError(wErr, "walk") + assert.Equal(n, len(foundFiles), "missing files") +}