Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix race condition due to concurrent use of rand source
A global rand.Source was being used concurrently, which is not safe.
  • Loading branch information
gammazero committed Feb 28, 2025
commit 3c2cdf065a49ed6db7c0094e37fb4cd35d26abfd
15 changes: 3 additions & 12 deletions flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,6 @@ var (
ErrInvalidKey = errors.New("key not supported by flatfs")
)

var (
r *rand.Rand
)

func init() {
r = rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
}

// Datastore implements the go-datastore Interface.
// Note this datastore cannot guarantee order of concurrent
// write operations to the same key. See the explanation in
Expand Down Expand Up @@ -862,11 +854,10 @@ func folderSize(path string, deadline time.Time) (int64, initAccuracy, error) {
}

// randomize file order
// https://stackoverflow.com/a/42776696
for i := len(files) - 1; i > 0; i-- {
j := r.Intn(i + 1)
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
r.Shuffle(len(files), func(i, j int) {
files[i], files[j] = files[j], files[i]
}
})

accuracy := exactA
for {
Expand Down