Skip to content

Data race in RandomString() #7

@vladimiroff

Description

@vladimiroff

When running tests which use this package with the race detector it finds out there are data races.
Trivial to reproduce example is with the following useless program:

package main

import "syreclabs.com/go/faker"

func main() {
	go faker.RandomString(10)
	go faker.RandomString(10)
}

When I run it with go run -race this is the output from go's race detector:

==================
WARNING: DATA RACE
Read at 0x00c42018afb8 by goroutine 7:
  math/rand.read()
      /usr/lib/go/src/math/rand/rand.go:265 +0x3c
  math/rand.(*Rand).Read()
      /usr/lib/go/src/math/rand/rand.go:261 +0x16d
  syreclabs.com/go/faker.RandomString()
      /home/kiril/go/src/syreclabs.com/go/faker/faker.go:90 +0xb2

Previous write at 0x00c42018afb8 by goroutine 6:
  math/rand.read()
      /usr/lib/go/src/math/rand/rand.go:276 +0xee
  math/rand.(*Rand).Read()
      /usr/lib/go/src/math/rand/rand.go:261 +0x16d
  syreclabs.com/go/faker.RandomString()
      /home/kiril/go/src/syreclabs.com/go/faker/faker.go:90 +0xb2

Goroutine 7 (running) created at:
  main.main()
      /tmp/fake.go:7 +0x6c

Goroutine 6 (finished) created at:
  main.main()
      /tmp/fake.go:6 +0x4b
==================
==================
WARNING: DATA RACE
Read at 0x00c42018afb0 by goroutine 7:
  math/rand.read()
      /usr/lib/go/src/math/rand/rand.go:266 +0x56
  math/rand.(*Rand).Read()
      /usr/lib/go/src/math/rand/rand.go:261 +0x16d
  syreclabs.com/go/faker.RandomString()
      /home/kiril/go/src/syreclabs.com/go/faker/faker.go:90 +0xb2

Previous write at 0x00c42018afb0 by goroutine 6:
  math/rand.read()
      /usr/lib/go/src/math/rand/rand.go:277 +0x108
  math/rand.(*Rand).Read()
      /usr/lib/go/src/math/rand/rand.go:261 +0x16d
  syreclabs.com/go/faker.RandomString()
      /home/kiril/go/src/syreclabs.com/go/faker/faker.go:90 +0xb2

Goroutine 7 (running) created at:
  main.main()
      /tmp/fake.go:7 +0x6c

Goroutine 6 (finished) created at:
  main.main()
      /tmp/fake.go:6 +0x4b
==================
Found 2 data race(s)
exit status 66

Now, this makes a lot of sense, since math/rand.Rand.Read is documented as not safe to be called concurrently. Which is okay for a method of type. RandomString in this package however is a global function and this issue could not be worked around as easily as with Rand.Read. For instance math/rand.Read (the global function) is safe to be called concurrently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions