diff --git a/mem/file.go b/mem/file.go index 9096ff0..3c1e09a 100644 --- a/mem/file.go +++ b/mem/file.go @@ -59,7 +59,9 @@ type FileData struct { modtime time.Time } -func (d FileData) Name() string { +func (d *FileData) Name() string { + d.Lock() + defer d.Unlock() return d.name } @@ -107,9 +109,7 @@ func (f *File) Close() error { } func (f *File) Name() string { - f.fileData.Lock() - defer f.fileData.Unlock() - return f.fileData.name + return f.fileData.Name() } func (f *File) Stat() (os.FileInfo, error) { diff --git a/memmap_test.go b/memmap_test.go index 44d525b..9efd1f2 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -259,3 +259,26 @@ func TestWriteCloseTime(t *testing.T) { } } } + +// This test should be run with the race detector on: +// go test -race -v -timeout 10s -run TestRacingDeleteAndClose +func TestRacingDeleteAndClose(t *testing.T) { + fs := NewMemMapFs() + pathname := "testfile" + f, err := fs.Create(pathname) + if err != nil { + t.Fatal(err) + } + + in := make(chan bool) + + go func() { + <-in + f.Close() + }() + go func() { + <-in + fs.Remove(pathname) + }() + close(in) +}