Ruby tempfile instances automatically delete their corresponding file when the references are released. However, I have one machine on which this is not the case. The code is
irb> require 'tempfile'
=> true
irb> t = Tempfile.new('test32')
=> #<File:/tmp/test32.27778.0>
irb> exit
on all of my test machines, this results in test32 getting deleted, except one. I have tried to delete a file using File.delete and unfortunately that works fine. Is there some ruby config I’m missing?
Ruby version is
ruby 1.8.6 (2009-06-08 patchlevel 369) [i686-linux].
Edit: Some additional information that has come to light in the conversation with DigitalRoss: If I explicitly release the Tempfile reference (t = nil), then the Tempfile gets cleaned up. Is is possible that the GC has been patched or altered in some way to need that?
Here’s some code that works on the “good” machines but on the “bad” machine it fails
include ObjectSpace
t = "blah"
define_finalizer(t, proc {|id| print "yes finalized id=#{id}", "\n" })
On the bad machine, the “yes finalized” only prints if I explicitly set t to nil.
OK, continuing the question’s comment thread…
Ruby, or really,
Tempfile, uses the garbage collector to manage finalizers. (I presume it works this way rather than via Kernel::at_exit in order to delete the file earlier in a long-running ruby.) Anyway, something seems different about GC on one system. Let’s try to pin it down. Try this, and see if clearing the only reference to the Tempfile instance and starting GC removes the file.