Skip to content

MemMapFS: race condition when creating directories #298

@acabarbaye

Description

@acabarbaye

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions