Panic: Concurrent map read and map write
crossworth opened this issue · comments
Hi, great project, but I'm facing a problem.
Expected Behaviour
I should be able to run the code without any panic's about concurrency.
Actual Behaviour
Sometimes I got a panic from a concurrent map read and map write.
Steps to Reproduce
Run this test with go test -race
https://github.com/crossworth/go-sx/blob/1c47103bc8c9989002193fe90c37571d0ba24167/concurrent_map_read_write_bug_test.go
package sx
import (
"fmt"
"testing"
)
func Test_matchingOf(t *testing.T) {
for i := 0; i < 1000; i++ {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
t.Parallel()
type aType struct {
Test string
}
var a aType
matchingOf(&a)
})
}
}
(is this the right way to test concurrency issues?)
Optional additional info:
Platform and Version
go version go1.16.4 windows/amd64
but the problem happens on linux/amd64 and darwin/amd64 as well.
Sample Code that illustrates the problem
Logs taken while reproducing problem
fatal error: concurrent map read and map write
goroutine 63 [running]:
runtime.throw(0x11293c2, 0x21)
C:/Program Files/Go/src/runtime/panic.go:1117 +0x79 fp=0xc003e41830 sp=0xc003e41800 pc=0x2cbf19
runtime.mapaccess2(0xe81160, 0xc0001a2330, 0xc003e41998, 0x35bd420, 0xf67160)
C:/Program Files/Go/src/runtime/map.go:469 +0x26c fp=0xc003e41870 sp=0xc003e41830 pc=0x2a062c
github.com/travelaudience/go-sx.matchingOf(0xe6f2c0, 0xc0029c8420, 0x20)
C:/Users/Pedro/code/project-name/vendor/github.com/travelaudience/go-sx/matching.go:62 +0x1d7 fp=0xc003e41a20 sp=0xc003e41870 pc=0x987d57
github.com/travelaudience/go-sx.Addrs(0xe6f2c0, 0xc0029c8420, 0xc003e41b20, 0x29f01f, 0x28)
C:/Users/Pedro/code/project-name/vendor/github.com/travelaudience/go-sx/helpers.go:227 +0x54 fp=0xc003e41ae0 sp=0xc003e41a20 pc=0x987874
github.com/travelaudience/go-sx.(*Rows).MustScans(0xc0005101d0, 0xe6f2c0, 0xc0029c8420)
C:/Users/Pedro/code/project-name/vendor/github.com/travelaudience/go-sx/tx.go:187 +0x45 fp=0xc003e41b30 sp=0xc003e41ae0 pc=0x988a65
...
I was able to fix the problem using a mutex:
https://github.com/crossworth/go-sx/commit/c17a11f021b45740238d429b24231191ca612e7e
Let me know if I should create a PR from the commit above.
Thank you so much for finding this! Yes, please create a PR, and we'll merge it.