ruby-changes:62876
From: Aaron <ko1@a...>
Date: Thu, 10 Sep 2020 04:46:02 +0900 (JST)
Subject: [ruby-changes:62876] 475c8701d7 (master): Make SecureRandom support Ractor
https://git.ruby-lang.org/ruby.git/commit/?id=475c8701d7 From 475c8701d74ebebefb2f53052cde1a5effb4cb81 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <tenderlove@r...> Date: Fri, 4 Sep 2020 17:32:31 -0700 Subject: Make SecureRandom support Ractor SecureRandom lazily defines `get_random`. Accessing the mutex to define the `get_random` method is not supported inside a Ractor. This commit defines `gen_random` when `securerandom` is required and makes it suppore Ractor (as well as thread safe). Here is a test program: ```ruby require "securerandom" r = Ractor.new do loop do Ractor.yield SecureRandom.hex end end p r.take ``` Before this commit: ``` $ make runruby ./miniruby -I./lib -I. -I.ext/common ./tool/runruby.rb --extout=.ext -- --disable-gems ./test.rb <internal:ractor>:38: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues. /Users/aaron/git/ruby/lib/securerandom.rb:94:in `gen_random': can not access instance variables of classes/modules from non-main Ractors (RuntimeError) <internal:ractor>:124:in `take': thrown by remote Ractor. (Ractor::RemoteError) from ./test.rb:9:in `<main>' /Users/aaron/git/ruby/lib/securerandom.rb:94:in `gen_random': can not access instance variables of classes/modules from non-main Ractors (RuntimeError) from /Users/aaron/git/ruby/lib/securerandom.rb:155:in `random_bytes' from /Users/aaron/git/ruby/lib/securerandom.rb:176:in `hex' from ./test.rb:5:in `block (2 levels) in <main>' from ./test.rb:4:in `loop' from ./test.rb:4:in `block in <main>' make: *** [runruby] Error ``` After this commit: ``` $ make runruby ./miniruby -I./lib -I. -I.ext/common ./tool/runruby.rb --extout=.ext -- --disable-gems ./test.rb <internal:ractor>:38: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues. "3fc8885157e3911bab4b5d7619bb0308" ``` diff --git a/lib/securerandom.rb b/lib/securerandom.rb index 205cb70..241fde9 100644 --- a/lib/securerandom.rb +++ b/lib/securerandom.rb @@ -66,42 +66,11 @@ https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L66 # module SecureRandom - @rng_chooser = Mutex.new # :nodoc: - class << self def bytes(n) return gen_random(n) end - def gen_random(n) - ret = Random.urandom(1) - if ret.nil? - begin - require 'openssl' - rescue NoMethodError - raise NotImplementedError, "No random device" - else - @rng_chooser.synchronize do - class << self - remove_method :gen_random - alias gen_random gen_random_openssl - public :gen_random - end - end - return gen_random(n) - end - else - @rng_chooser.synchronize do - class << self - remove_method :gen_random - alias gen_random gen_random_urandom - public :gen_random - end - end - return gen_random(n) - end - end - private def gen_random_openssl(n) @@ -129,6 +98,21 @@ module SecureRandom https://github.com/ruby/ruby/blob/trunk/lib/securerandom.rb#L98 end ret end + + ret = Random.urandom(1) + if ret.nil? + begin + require 'openssl' + rescue NoMethodError + raise NotImplementedError, "No random device" + else + alias gen_random gen_random_openssl + end + else + alias gen_random gen_random_urandom + end + + public :gen_random end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/