Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MemMapFS: race condition when creating directories #298

Closed
acabarbaye opened this issue Apr 8, 2021 · 0 comments · Fixed by #379
Closed

MemMapFS: race condition when creating directories #298

acabarbaye opened this issue Apr 8, 2021 · 0 comments · Fixed by #379
Labels

Comments

@acabarbaye
Copy link

When trying to create a same folder concurrently, one Mkdir should return an error stating the path already exists. MemMapFS works a lot of the time as expected but sometimes the error is not returned.
See below, a test case to reproduce the problem:

import (
	"github.com/spf13/afero"
	"github.com/stretchr/testify/require"
	"testing"
)

func TestMemMap(t *testing.T) {
	for i := 0; i < 100; i++ {
		fs := afero.NewMemMapFs()
		dir := "testDir"
		c1 := make(chan error)
		c2 := make(chan error)

		go func() {
			c1 <- fs.Mkdir(dir, 0755)
		}()

		go func() {
			c2 <- fs.Mkdir(dir, 0755)
		}()

		// Only one attempt of creating the directory should succeed.
		err1 := <-c1
		err2 := <-c2
		require.NotEqual(t, err1, err2, "run #%v", i)
	}
}

The failure can happen at any time
e.g.

=== RUN   TestMemMap
    TestMemMap: test_test.go:27: 
        	Error Trace:	test_test.go:27
        	Error:      	Should not be: <nil>
        	Test:       	TestMemMap
        	Messages:   	run #17
--- FAIL: TestMemMap (0.00s)
@bep bep added the bug label Nov 14, 2022
bep added a commit to bep/afero that referenced this issue Nov 14, 2022
* 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 spf13#361
Fixes spf13#298
bep added a commit to bep/afero that referenced this issue Nov 14, 2022
* 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 spf13#361
Fixes spf13#298
bep added a commit to bep/afero that referenced this issue Nov 14, 2022
* 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 spf13#361
Fixes spf13#298
@bep bep closed this as completed in #379 Nov 14, 2022
@bep bep closed this as completed in a800a9d Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants