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
658 stars 31 forks source link

Concurrent map writes error when calling faker in parrallel #5

Closed huydinhle closed 2 years ago

huydinhle commented 2 years ago
package main

import (
    "fmt"
  "sync"
  "github.com/go-faker/faker/v4"

)

func main() {
    var wg sync.WaitGroup
    wg.Add(1000)
    fmt.Println("Running for loop…")
    for i := 0; i < 1000; i++ {
        go func(i int) {
            defer wg.Done()
      fmt.Println(  fmt.Sprintf("%s-%s",faker.FirstName(),faker.LastName()) )
        }(i)
    }
    wg.Wait()
    fmt.Println("Finished for loop")
}

Error

Running for loop…
fatal error: concurrent map writes
Vernie-Thiel

goroutine 4 [running]:
runtime.throw({0x102820cb4?, 0x18?})
        /usr/local/go/src/runtime/panic.go:992 +0x50 fp=0x1400005bce0 sp=0x1400005bcb0 pc=0x1027749a0
runtime.mapassign_faststr(0x0?, 0x0?, {0x10281d2c7, 0xb})
        /usr/local/go/src/runtime/map_faststr.go:212 +0x3bc fp=0x1400005bd50 sp=0x1400005bce0 pc=0x1027558ac
github.com/go-faker/faker/v4.initMapperTagWithOption({0x0, 0x0, 0x0})
        /Users/hle/pkg/mod/github.com/go-faker/faker/v4@v4.0.0-beta.2/faker.go:220 +0x230 fp=0x1400005be60 sp=0x1400005bd50 pc=0x10280a2e0
github.com/go-faker/faker/v4.initOption({0x0, 0x0, 0x0})
        /Users/hle/pkg/mod/github.com/go-faker/faker/v4@v4.0.0-beta.2/faker.go:232 +0x40 fp=0x1400005be90 sp=0x1400005be60 pc=0x10280ab40
github.com/go-faker/faker/v4.singleFakeData({0x10281ccce, 0xa}, 0x1028749f0, {0x0?, 0x0?, 0x0?})
        /Users/hle/pkg/mod/github.com/go-faker/faker/v4@v4.0.0-beta.2/faker.go:1263 +0x3c fp=0x1400005bec0 sp=0x1400005be90 pc=0x10280b57c
github.com/go-faker/faker/v4.FirstName({0x0?, 0x0?, 0x0?})
        /Users/hle/pkg/mod/github.com/go-faker/faker/v4@v4.0.0-beta.2/person.go:227 +0x44 fp=0x1400005bf00 sp=0x1400005bec0 pc=0x10280d834
main.main.func1(0x0?)
        /Users/hle/src/github.com/huydinhle/tmp/go-playground/main.go:17 +0x5c fp=0x1400005bfb0 sp=0x1400005bf00 pc=0x102814d2c
main.main.func2()
        /Users/hle/src/github.com/huydinhle/tmp/go-playground/main.go:18 +0x34 fp=0x1400005bfd0 sp=0x1400005bfb0 pc=0x102814c94
runtime.goexit()
        /usr/local/go/src/runtime/asm_arm64.s:1263 +0x4 fp=0x1400005bfd0 sp=0x1400005bfd0 pc=0x1027a0c94
created by main.main
        /Users/hle/src/github.com/huydinhle/tmp/go-playground/main.go:15 +0x8c

goroutine 1 [runnable]:
main.main()
        /Users/hle/src/github.com/huydinhle/tmp/go-playground/main.go:15 +0xac
bxcodec commented 2 years ago

It will be fixed in the #6

bxcodec commented 2 years ago

@huydinhle can you try again with the new version v4.0.0-beta.3