From 5aec2c99b81f28d2adf64d2f4bad0b94ecea2e65 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 4 Dec 2025 10:17:20 -0500 Subject: [PATCH] fix rand_bytes: init at least the local domain's Rand state --- src/core/rand_bytes.ml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/core/rand_bytes.ml b/src/core/rand_bytes.ml index 336020a5..c90c1a72 100644 --- a/src/core/rand_bytes.ml +++ b/src/core/rand_bytes.ml @@ -1,4 +1,12 @@ +let initialized_ = Atomic.make false + +let[@inline never] actually_init () = Random.self_init () + +let[@inline] maybe_init () = + if not (Atomic.exchange initialized_ true) then actually_init () + let default_rand_bytes_8 () : bytes = + maybe_init (); let b = Bytes.create 8 in for i = 0 to 1 do (* rely on the stdlib's [Random] being thread-or-domain safe *) @@ -14,6 +22,7 @@ let default_rand_bytes_8 () : bytes = b let default_rand_bytes_16 () : bytes = + maybe_init (); let b = Bytes.create 16 in for i = 0 to 4 do (* rely on the stdlib's [Random] being thread-or-domain safe *)