Skip to content

Commit

Permalink
Avoid race condition in random library
Browse files Browse the repository at this point in the history
  • Loading branch information
moskyb committed Mar 13, 2023
1 parent 2ca3dc9 commit 09ec094
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions jobapi/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"time"
)

var random = rand.New(rand.NewSource(time.Now().UnixNano()))
func init() {
rand.Seed(time.Now().UnixNano())
}

// NewSocketPath generates a path to a socket file (without actually creating the file itself) that can be used with the
// job api.
Expand All @@ -19,6 +21,6 @@ func NewSocketPath(base string) (string, error) {
return "", fmt.Errorf("creating socket directory: %w", err)
}

sockNum := random.Int63() % 100_000
sockNum := rand.Int63() % 100_000
return filepath.Join(path, fmt.Sprintf("%d-%d.sock", os.Getpid(), sockNum)), nil
}

0 comments on commit 09ec094

Please sign in to comment.