--- timeout.rb.orig 2011-01-17 17:55:30.070125000 +0100 +++ timeout.rb 2011-01-17 18:35:28.976375000 +0100 @@ -21,7 +21,7 @@ # # Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc. # Copyright:: (C) 2000 Information-technology Promotion Agency, Japan - +require 'thread' module Timeout # Raised by Timeout#timeout when the block times out. class Error < RuntimeError @@ -45,14 +45,19 @@ exception = klass || Class.new(ExitException) begin x = Thread.current - y = Thread.start { - begin - sleep sec - rescue => e - x.raise e - else - x.raise exception, "execution expired" if x.alive? - end + m = Mutex.new + m.synchronize { + y = Thread.start { + m.synchronize {} # ensure variable y is pointing to the thread + # before y starts the timer + begin + sleep sec + rescue => e + x.raise e + else + x.raise exception, "execution expired" if x.alive? + end + } } return yield(sec) rescue exception => e