diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | random.c | 1 | ||||
-rw-r--r-- | test/ruby/test_rand.rb | 33 | ||||
-rw-r--r-- | version.h | 2 |
4 files changed, 38 insertions, 3 deletions
@@ -1,3 +1,8 @@ +Tue Oct 16 10:47:43 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * random.c (random_s_rand): ensure default PRNG is re-initialized + after fork. patched by Eric Wong. [ruby-core:41209][Bug #5661] + Tue Oct 16 10:21:23 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> * ruby.c (rb_f_sub, rb_f_gsub): pass the given block. @@ -1255,6 +1255,7 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj) static VALUE random_s_rand(int argc, VALUE *argv, VALUE obj) { + rand_start(&default_rand); return random_rand(argc, argv, rb_Random_DEFAULT); } diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb index 67b6df8562..c7139818de 100644 --- a/test/ruby/test_rand.rb +++ b/test/ruby/test_rand.rb @@ -415,14 +415,43 @@ END def test_fork_shuffle pid = fork do - (1..10).to_a.shuffle - raise 'default seed is not set' if srand == 0 + (1..10).to_a.shuffle + raise 'default seed is not set' if srand == 0 end p2, st = Process.waitpid2(pid) assert(st.success?, "#{st.inspect}") rescue NotImplementedError, ArgumentError end + def assert_fork_status(n, mesg, &block) + IO.pipe do |r, w| + (1..n).map do + p1 = fork {w.puts(block.call.to_s)} + _, st = Process.waitpid2(p1) + assert_send([st, :success?], mesg) + r.gets.strip + end + end + end + + def test_rand_reseed_on_fork + bug5661 = '[ruby-core:41209]' + + assert_fork_status(1, bug5661) {Random.rand(4)} + r1, r2 = *assert_fork_status(2, bug5661) {Random.rand} + assert_not_equal(r1, r2, bug5661) + + assert_fork_status(1, bug5661) {rand(4)} + r1, r2 = *assert_fork_status(2, bug5661) {rand} + assert_not_equal(r1, r2, bug5661) + + stable = Random.new + assert_fork_status(1, bug5661) {stable.rand(4)} + r1, r2 = *assert_fork_status(2, bug5661) {stable.rand} + assert_equal(r1, r2, bug5661) + rescue NotImplementedError + end + def test_seed bug3104 = '[ruby-core:29292]' rand_1 = Random.new(-1).rand @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 292 +#define RUBY_PATCHLEVEL 293 #define RUBY_RELEASE_DATE "2012-10-16" #define RUBY_RELEASE_YEAR 2012 |