Repository: syncthing/syncthing (language: go)

Issue:
Data race in fakeFS (testing)
### What happened?

```
=== RUN   TestRecvOnlyRemoteUndoChanges
==================
WARNING: DATA RACE
Read at 0x00c0003546b0 by goroutine 360:
  github.com/syncthing/syncthing/lib/fs.(*fakeFile).Stat()
      /home/runner/work/syncthing/syncthing/lib/fs/fakefs.go:952 +0x5d
  github.com/syncthing/syncthing/lib/fs.mtimeFile.Stat()
      /home/runner/work/syncthing/syncthing/lib/fs/mtimefs.go:209 +0x5d
  github.com/syncthing/syncthing/lib/fs.(*mtimeFile).Stat()
      <autogenerated>:1 +0x59
  github.com/syncthing/syncthing/lib/fs.(*metricsFile).Stat()
      /home/runner/work/syncthing/syncthing/lib/fs/metrics.go:304 +0xfa
  github.com/syncthing/syncthing/lib/scanner.HashFile()
      /home/runner/work/syncthing/syncthing/lib/scanner/blockqueue.go:50 +0x776
  github.com/syncthing/syncthing/lib/scanner.(*parallelHasher).hashFiles()
      /home/runner/work/syncthing/syncthing/lib/scanner/blockqueue.go:111 +0x4eb
  github.com/syncthing/syncthing/lib/scanner.newParallelHasher.func1()
      /home/runner/work/syncthing/syncthing/lib/scanner/blockqueue.go:89 +0x4f

Previous write at 0x00c0003546b0 by goroutine 311:
  github.com/syncthing/syncthing/lib/fs.(*fakeFS).Chmod()
      /home/runner/work/syncthing/syncthing/lib/fs/fakefs.go:235 +0x15b
  github.com/syncthing/syncthing/lib/fs.(*caseFilesystem).Chmod()
      /home/runner/work/syncthing/syncthing/lib/fs/casefs.go:153 +0x97
  github.com/syncthing/syncthing/lib/fs.(*metricsFS).Chmod()
      /home/runner/work/syncthing/syncthing/lib/fs/metrics.go:107 +0x107
  github.com/syncthing/syncthing/lib/fs.(*walkFilesystem).Chmod()
      <autogenerated>:1 +0x6a
  github.com/syncthing/syncthing/lib/model.writeFilePerm()
      /home/runner/work/syncthing/syncthing/lib/model/testutils_test.go:436 +0xe1
  github.com/syncthing/syncthing/lib/model.TestRecvOnlyRemoteUndoChanges()
      /home/runner/work/syncthing/syncthing/lib/model/folder_recvonly_test.go:381 +0xaa9
  testing.tRunner()
      /opt/hostedtoolcache/go/1.21.9/x64/src/testing/testing.go:1595 +0x261
  testing.(*T).Run.func1()
      /opt/hostedtoolcache/go/1.21.9/x64/src/testing/testing.go:1648 +0x44

Goroutine 360 (running) created at:
  github.com/syncthing/syncthing/lib/scanner.newParallelHasher()
      /home/runner/work/syncthing/syncthing/lib/scanner/blockqueue.go:89 +0x32f
  github.com/syncthing/syncthing/lib/scanner.(*walker).walk.func1()
      /home/runner/work/syncthing/syncthing/lib/scanner/walk.go:170 +0x33d

Goroutine 311 (running) created at:
  testing.(*T).Run()
      /opt/hostedtoolcache/go/1.21.9/x64/src/testing/testing.go:1648 +0x845
  testing.runTests.func1()
      /opt/hostedtoolcache/go/1.21.9/x64/src/testing/testing.go:2054 +0x84
  testing.tRunner()
      /opt/hostedtoolcache/go/1.21.9/x64/src/testing/testing.go:1595 +0x261
  testing.runTests()
      /opt/hostedtoolcache/go/1.21.9/x64/src/testing/testing.go:2052 +0x8ad
  testing.(*M).Run()
      /opt/hostedtoolcache/go/1.21.9/x64/src/testing/testing.go:1925 +0xcd7
  main.main()
      _testmain.go:369 +0x2bd
==================
    testing.go:1465: race detected during execution of test
--- FAIL: TestRecvOnlyRemoteUndoChanges (0.02s)
```

### Syncthing version

v1.27.5

### Platform & operating system

-

### Browser version

_No response_

### Relevant log output

_No response_


Discussion / hints:

Fix patch:
diff --git a/lib/fs/fakefs.go b/lib/fs/fakefs.go
index 65151dbc35e..05c86200cd4 100644
--- a/lib/fs/fakefs.go
+++ b/lib/fs/fakefs.go
@@ -924,6 +924,8 @@ func (f *fakeFile) Name() string {
 	if f.presentedName != "" {
 		return f.presentedName
 	}
+	f.mut.Lock()
+	defer f.mut.Unlock()
 	return f.name
 }
 
@@ -949,7 +951,9 @@ func (f *fakeFile) Truncate(size int64) error {
 }
 
 func (f *fakeFile) Stat() (FileInfo, error) {
+	f.mut.Lock()
 	info := &fakeFileInfo{*f.fakeEntry}
+	f.mut.Unlock()
 	if f.presentedName != "" {
 		info.name = f.presentedName
 	}


Test patch: