From: Paul Brannan Date: 2004-08-30T22:43:22+09:00 Subject: Re: Request For Comments: exception safe ConditionVariable#wait On Sat, Aug 28, 2004 at 05:38:53PM +0900, Bill Kelly wrote: > Wow, nice catch! Thanks - ! > > Hmm... Would that mean it's possible for > > ensure > # -------> %exception here% > mutex.lock if unlocked > end > > potentially an exception firing once we're in the > ensure block as well? Or is ensure implemented to > have the properties of your no_raise style block, > I wonder? Actually the exception has the potential to occur when calling the Mutex#lock method, if Ruby decides to switch threads just at the point of the call (so it's not really between the ensure and the next statement). > Wow your observation has really got me curious now: > Can the exception raised by timeout occur anywhere > in: > - a rescue block? > - an ensure block? I'm not an expert on the Ruby interpreter, but if I understand eval.c correctly, it can occur any time Ruby can switch threads, which includes but is not limited to: - each time a node is evaluated - each time (just before) a method is called - each time an extension waits for IO using rb_thread_select > That would seem to make it really hard to guard > against... I agree. I've never really liked the thread-based timeout for exactly that reason (that and my C++ extensions don't always play nicely with Ruby threads, since they keep data on the stack). Instead I use an event loop, and let the event loop handle my timeouts; this guarantees that timeout exceptions are only raised from well-defined points in the code. Paul