Project

General

Profile

« Previous | Next » 

Revision 475c8701

Added by tenderlovemaking (Aaron Patterson) almost 5 years ago

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:

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"