From: Eric Hodel Date: 2007-12-25T10:14:29+09:00 Subject: Re: [PATCH] Timeout.timeout expires early - breaks RubyGems On Dec 24, 2007, at 16:36 PM, Eric Hodel wrote: > Sometimes Timeout.timeout expires early. I see this with `gem > install` frequently because it fetches a large file. > > The following test fetches a 10MB file from http://izmunuti.segment7.net/~drbrain/zeros > (all NULL) and shows a Timeout::Error being raised after an > impossibly small timeout. > > Occasionally the fetch will complete, but usually the errors are > between 150 and 300 iterations of the loop. Sometimes it seems that the timeout thread is woken early. This patch fixes the problem. Index: lib/timeout.rb =================================================================== --- lib/timeout.rb (revision 14640) +++ lib/timeout.rb (working copy) @@ -40,7 +40,8 @@ module Timeout begin x = Thread.current y = Thread.start { - sleep sec + slept = 0 + slept += sleep sec until slept >= sec x.raise exception, "execution expired" if x.alive? } return yield(sec)