Patches: Browse | Submit New | Admin

[#29627] Fixed segfault by seeding random number generator in child process

Date:
2012-08-08 17:59
Priority:
3
Submitted By:
Andrew Havens (andrewhavens)
Assigned To:
Thomas Uehlinger (uehli)
Category:
None
State:
Closed
Summary:
Fixed segfault by seeding random number generator in child process

Detailed description
I came across a segmentation fault while using the delayed job gem (https://github.com/collectiveidea/delayed_job).
It uses the daemons gem to daemonize the job worker. One of my jobs was calling Random.rand() to generate a random number,
but it was causing a segfault. But once I changed it to Random.new.rand() it fixed it. I think this is because calling
Random.rand() expects that it is seeded with srand() before being called. Digging through the daemons source I found
that Daemonize.daemonize() starts by calling srand(), but Daemonize.call_as_daemon() does not. Adding srand() at the
start of the child process seemed to fix the problem.

Here's a repeatable test to verify the fix:


require 'daemons'

Daemons.run_proc('myscript') do

loop do
  file = File.open('/tmp/myscript.log', 'a')
  file.write(Random.rand)   # breaks without seeding
  # file.write(Random.new.rand)  # works without seeding
  # file.write(rand) # also works, but this is Kernel.rand() so its different
  file.write("\n")
  file.close()
  sleep 2
end

end

Add A Comment: Notepad

Please login


Followup

Message
Date: 2012-08-10 08:34
Sender: Thomas Uehlinger

Andrew,

thanks for the report and thorough investigation. I just pushed
out a bugfix release (1.1.9) with your fix.

Thanks again,

Thomas

Attached Files:

Name Description Download
daemonize.rb.patch adds srand to call_as_daemon Download

Changes:

Field Old Value Date By
close_date2012-08-10 08:352012-08-10 08:35uehli
status_idOpen2012-08-10 08:35uehli
assigned_tonone2012-08-10 08:34uehli
File Added5616: daemonize.rb.patch2012-08-08 17:59andrewhavens