From: Joel VanderWerf Date: 2006-09-18T07:27:49+09:00 Subject: Re: timeout and ensure Arnaud Bergeron wrote: > On 9/17/06, khaines@enigo.com wrote: >> On Mon, 18 Sep 2006, Joel VanderWerf wrote: >> >> > I wish I had an easy answer to this problem, but I don't. Are >> timeout and >> > ensure inherently incompatible? >> >> I don't have an answer, but what if you make your ensure section into a >> critical section? >> >> ensure >> Thread.critical = true >> # do stuff >> Thread.critical = false >> end >> >> >> Kirk Haines > > There is still the race problem if the exception gets raised just > before the Thread.critical. Confirmed by experiment with ruby 1.8.4: $ cat ensure-timeout.rb require 'timeout' Thread.abort_on_exception = true work_thread = Thread.new do loop do begin Thread.pass ensure Thread.critical = true begin rescue Timeout::Error puts "caught a Timeout::Error" end Thread.critical = false #retry end end end loop do work_thread.raise Timeout::Error end $ ruby ensure-timeout.rb ensure-timeout.rb:22: Timeout::Error (Timeout::Error) from ensure-timeout.rb:6 from ensure-timeout.rb:5 -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407