Skip to content

Commit

Permalink
cache RandomDevice file from __init__
Browse files Browse the repository at this point in the history
This prevents us from seeing an invalid `IOStream` object from a saved
system image, and also ensures the files are initialized once for all
threads.
  • Loading branch information
JeffBezanson committed Oct 7, 2021
1 parent 7782330 commit 0599496
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions stdlib/Random/src/RNGs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ else # !windows

function getfile(rd::RandomDevice)
devrandom = rd.unlimited ? DEV_URANDOM : DEV_RANDOM
# TODO: there is a data-race, this can leak up to nthreads() copies of the file descriptors,
# so use a "thread-once" utility once available
isassigned(devrandom) || (devrandom[] = open(rd.unlimited ? "/dev/urandom" : "/dev/random"))
devrandom[]
end

Expand Down Expand Up @@ -411,6 +408,10 @@ for T in BitInteger_types
end

function __init__()
@static if !Sys.iswindows()
DEV_RANDOM[] = open("/dev/random")
DEV_URANDOM[] = open("/dev/urandom")
end
seed!(GLOBAL_RNG)
end

Expand Down

0 comments on commit 0599496

Please sign in to comment.