Jacob Maine's blog



Jacob MaineJacob Maine
SF Standup 3/1/2010: JRuby skips ensure blocks when killed
edit Posted by Jacob Maine on Monday March 01, 2010 at 09:18AM

When you kill a JRuby process (e.g. with a SIGINT) you can't bank on ensure blocks being executed. Of course, this is worrisome - file handles and other connections won't be closed. The problem boils down to this example:

$ cat kill_me.rb 
begin
  sleep
ensure
  puts 'executed ensure'
end
$ 
$ ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
$
$ ruby kill_me.rb 
^Cexecuted ensure
kill_me.rb:2:in `sleep': Interrupt
    from kill_me.rb:2
$
$ jruby -v
jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_13) [x86_64-java]
$
$ jruby kill_me.rb 
^C$