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
|