Project

General

Profile

Backport #5661 ยป 0001-random.c-random_s_rand-avoid-segfault-after-fork-rub.patch

normalperson (Eric Wong), 11/23/2011 05:45 AM

View differences:

random.c
1295 1295
static VALUE
1296 1296
random_s_rand(int argc, VALUE *argv, VALUE obj)
1297 1297
{
1298
    rand_start(&default_rand);
1299

  
1298 1300
    return random_rand(argc, argv, rb_Random_DEFAULT);
1299 1301
}
1300 1302

  
test/ruby/test_rand.rb
423 423
  rescue NotImplementedError, ArgumentError
424 424
  end
425 425

  
426
  def fork_check(op = nil, &block)
427
    r, w = IO.pipe
428

  
429
    p1 = fork { w.puts(block.call.to_s) }
430
    _, st = Process.waitpid2(p1)
431
    assert st.success?, st.inspect
432
    r1 = r.gets
433

  
434
    p2 = fork { w.puts(block.call.to_s) }
435
    _, st = Process.waitpid2(p2)
436
    assert st.success?, st.inspect
437
    r2 = r.gets
438

  
439
    # technically, it *is* possible to fail this test _very_ rarely...
440
    assert_operator(r1.strip, op, r2.strip) if op
441
  rescue NotImplementedError
442
  ensure
443
    r.close if r
444
    w.close if w
445
  end
446

  
447
  def test_rand_reseed_on_fork
448
    # Bug 5661 / [ruby-core:41209]
449
    fork_check { Random.rand(4) }
450
    fork_check(:!=) { Random.rand }
451
    fork_check { rand(4) }
452
    fork_check(:!=) { rand }
453
    stable = Random.new
454
    fork_check(:==) { stable.rand(4) }
455
    fork_check(:==) { stable.rand }
456
  end
457

  
426 458
  def test_seed
427 459
    bug3104 = '[ruby-core:29292]'
428 460
    rand_1 = Random.new(-1).rand