"ko1 (Koichi Sasada)" <redmine / ruby-lang.org> wrote:
> * Un-safe ensure clause: Generally, ensure clause should not interrupt
> because it contains important tasks such as freeing resources.

Thank you for addressing this issue.  ensure clause behaving properly
is most important to me.

>     # example: Guard from Thread#raise
>     th = Thread.new do
>       Thead.control_interrupt(RuntimeError => :never) {
>         begin
>           # Thread#raise doesn't interrupt here.
>           # You can write resource allocation code safely.
>           Thread.control_interrupt(RuntimeError => :immediate) {
>             # ...
>             # It is possible to be interrupted by Thread#raise.
>           }
>         ensure
>           # Thread#raise doesn't interrupt here.
>           # You can write resource dealocation code safely.
>         end
>       }
>     end

I like the above is now possible and safe, but I think having
Thread.control_interrupt twice in above example is repetitive
and error-prone.

How about having something like at_exit, but local to the current scope:

  def something
    at_scope_exit do
      # Thread#raise doesn't interrupt here.
      # do what you would normally do in ensure clause
      # deallocate resource if allocated
      res.release if res
    end

    # It is possible to be interrupted by Thread#raise.
    # You can write resource allocation code safely because
    # at_scope_exit already registered deallocation code
    res = Foo.acquire
    ...

  end

at_scope_exit could probably take the same args as
Thread.control_interrupt, too:

  at_scope_exit(TimeoutError => :never) { ... }