Hi, We recently noticed random numbers from the Unicorn workers were turning up lots of duplicates across requests. Is it possible that the random seed generated when the ruby interpreter starts is duplicated when the workers are forked, and not re-seeded after fork? If so, should I call Kernel.srand in after_fork, or is this something which should be added to Unicorn? -Greg
ghazel at gmail.com wrote:> Hi, > > We recently noticed random numbers from the Unicorn workers were > turning up lots of duplicates across requests. Is it possible that the > random seed generated when the ruby interpreter starts is duplicated > when the workers are forked, and not re-seeded after fork? > > If so, should I call Kernel.srand in after_fork, or is this something > which should be added to Unicorn?It looks like a 1.8 bug, which version of Ruby are you running? Ruby 1.9.1 actually had a bug where it was reseeded improperly and led to segfaults (see KNOWN_ISSUES). 1.9.2 is fine afaik. -- Eric Wong
On Fri, Jan 28, 2011 at 2:26 AM, Eric Wong <normalperson at yhbt.net> wrote:> ghazel at gmail.com wrote: >> Hi, >> >> We recently noticed random numbers from the Unicorn workers were >> turning up lots of duplicates across requests. Is it possible that the >> random seed generated when the ruby interpreter starts is duplicated >> when the workers are forked, and not re-seeded after fork? >> >> If so, should I call Kernel.srand in after_fork, or is this something >> which should be added to Unicorn? > > It looks like a 1.8 bug, which version of Ruby are you running? ?Ruby > 1.9.1 actually had a bug where it was reseeded improperly and led to > segfaults (see KNOWN_ISSUES). ?1.9.2 is fine afaik.REE 1.8.7 So, calling Kernel.srand in after_fork will fix the 1.8 bug and the 1.9.1 bug and work fine on 1.9.2? -Greg
ghazel at gmail.com wrote:> So, calling Kernel.srand in after_fork will fix the 1.8 bug and the > 1.9.1 bug and work fine on 1.9.2?Yes. The 1.9.1 bug is fixed in p429 AFAIK, but I seem to recall p429 being completely broken with gems. Just use 1.9.2 if you''re on 1.9 -- Eric Wong
Eric Wong <normalperson at yhbt.net> wrote:> ghazel at gmail.com wrote: > > So, calling Kernel.srand in after_fork will fix the 1.8 bug and the > > 1.9.1 bug and work fine on 1.9.2?Just reported the bug to ruby-core and pushed this out:>From f4caf6b6bdea902abaadd3c04b2af94f056c4ff1 Mon Sep 17 00:00:00 2001From: Eric Wong <normalperson at yhbt.net> Date: Fri, 28 Jan 2011 18:11:26 +0000 Subject: [PATCH] KNOWN_ISSUES: document broken RNG+fork in newer Ruby 1.8 Reported by: ghazel at gmail.com ref: <AANLkTimTpPATTpkoD2EYA2eM1+5OzCN=WxnCygQmJdhn at mail.gmail.com> --- KNOWN_ISSUES | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-) diff --git a/KNOWN_ISSUES b/KNOWN_ISSUES index f218b83..259681d 100644 --- a/KNOWN_ISSUES +++ b/KNOWN_ISSUES @@ -3,6 +3,11 @@ Occasionally odd {issues}[link:ISSUES.html] arise without a transparent or acceptable solution. Those issues are documented here. +* Under some versions of Ruby 1.8, it is necessary to call +srand+ in an + after_fork hook to get correct random number generation. + + See http://redmine.ruby-lang.org/issues/show/4338 + * For notes on sandboxing tools such as Bundler or Isolate, see the {Sandbox}[link:Sandbox.html] page. -- Eric Wong