From 05994964f74ad1da7b36b3bb4f2ddbb5c105a7df Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 7 Oct 2021 16:30:36 -0400 Subject: [PATCH] cache `RandomDevice` file from `__init__` 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. --- stdlib/Random/src/RNGs.jl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stdlib/Random/src/RNGs.jl b/stdlib/Random/src/RNGs.jl index 141ec14f4ed31f..4598be29b8b954 100644 --- a/stdlib/Random/src/RNGs.jl +++ b/stdlib/Random/src/RNGs.jl @@ -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 @@ -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