go-faker / faker

Go (Golang) Fake Data Generator for Struct, previously https://github.com/bxcodec/faker
https://pkg.go.dev/github.com/go-faker/faker/v4
MIT License
614 stars 30 forks source link

concurrent map writes happened while use "options.WithGenerateUniqueValues(true)" #29

Closed Milk-Tang closed 4 weeks ago

Milk-Tang commented 1 year ago

while I'm using faker.Username(options.WithGenerateUniqueValues(true)) in a concurrent scenario, "concurrent map writes" happened in the func generateUniquehttps://github.com/go-faker/faker/blob/91fd09b0f81ca8520ead46dac24425c5db029861/faker.go#L1328-L1337

bxcodec commented 1 year ago

Can you give me your test scenario? How to reproduce it here? What are the usecases?

bxcodec commented 1 year ago
uniqueValues[dataType] = append(uniqueValues[dataType], value) 

This line definitely not safe for concurrent

Milk-Tang commented 1 year ago

Can you give me your test scenario? How to reproduce it here? What are the usecases?

Simply put, I used the faker. Username (options. WithGenerateUniqueValues (true)) method in concurrent use cases

ornj commented 1 month ago

Happening in v4.4.1. Here's a a redacted panic occurring when tests generating fake IP addresses are run with t.Parallel()

fatal error: concurrent map writes

goroutine 32 [running]:
github.com/go-faker/faker/v4.generateUnique({0x102a6477f, 0x4}, 0x140001a8dc8)
    /Users/ornj/go/pkg/mod/github.com/go-faker/faker/v4@v4.4.1/faker.go:1384 +0x198
github.com/go-faker/faker/v4.singleFakeData({0x102a6477f, 0x4}, 0x140001a8dc8, {0x140001a8db8?, 0x140001a8db8?, 0x1027b0cf4?})
    /Users/ornj/go/pkg/mod/github.com/go-faker/faker/v4@v4.4.1/faker.go:1394 +0x68
github.com/go-faker/faker/v4.IPv6(...)
    /Users/ornj/go/pkg/mod/github.com/go-faker/faker/v4@v4.4.1/internet.go:239

...

testing.tRunner(0x140001a24e0, 0x140000d26a0)
    /usr/local/go/1.21.4/libexec/src/testing/testing.go:1595 +0xe8
created by testing.(*T).Run in goroutine 23
    /usr/local/go/1.21.4/libexec/src/testing/testing.go:1648 +0x33c
bxcodec commented 4 weeks ago

Hi @Milk-Tang and @ornj

Thanks for the issue, and I'm sorry for the late update. Can you try v4.4.2, it should be fixed.