Forum: JRuby finalizers be called at interrupt?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2012-06-14 21:53
Hello all.

I noticed that in MRI, this code:

def finalized_puts bad, benign
    ObjectSpace.define_finalizer(bad) { puts benign.to_s }

n = 0
loop {
  a = [3]
  finalized_puts a, n
  n += 1
  print '.'

If I hit 'ctrl-c' to interrupt it, in MRI the finalizers are run, but no
in Jruby.  Is this expected?  Will file a bug report eventually if no
5993756d2b69ce0bc16df3318c240d54?d=identicon&s=25 Wayne Meissner (Guest)
on 2012-06-14 23:05
(Received via mailing list)
References won't be collected immediately upon interrupt (and
therefore the finalizers won't be run) - it'll happen when the GC
needs to reclaim memory.

But, won't your finalized_puts bind the 'bad' param in the block, and
stop it from being collected? (i.e. the block will always have a ref
to the bad instance, and won't allow it to be collected).

if you change it to something like this, it might work:

def finalized_puts bad, benign
  ObjectSpace.define_finalizer(bad, create_finalizer(benign))

def create_finalizer(benign) { |bad| puts benign.to_s }
This topic is locked and can not be replied to.