Where does Ruby keep track of its open file descriptors?
Asked Answered
P

1

25

What This Question Is Not About

This question is not about how to auto-close a file with File#close or the File#open block syntax. It's a question about where Ruby stores its list of open file descriptors at runtime.

The Actual Question

If you have a program with open descriptors, but you don't have access to the related File or IO object, how can you find a reference to the currently-open file descriptors? Take this example:

filename='/tmp/foo'
%x( touch "#{filename}" )
File.open(filename)
filehandle = File.open(filename)

The first File instance is opened, but the reference to the object is not stored in a variable. The second instance is stored in filehandle, where I can easily access it with #inspect or #close.

However, the discarded File object isn't gone; it's just not accessible in any obvious way. Until the object is finalized, Ruby must be keeping track of it somewhere...but where?

Palikar answered 6/6, 2012 at 20:48 Comment(0)
P
41

TL; DR

All File and IO objects are stored in ObjectSpace.

Answer

The ObjectSpace class says:

The ObjectSpace module contains a number of routines that interact with the garbage collection facility and allow you to traverse all living objects with an iterator.

How I Tested This

I tested this at the console on Ruby 1.9.3p194.

The test fixture is really simple. The idea is to have two File objects with different object identities, but only one is directly accessible through a variable. The other is "out there somewhere."

# Don't save a reference to the first object.
filename='/tmp/foo'
File.open(filename)
filehandle = File.open(filename)

I then explored different ways I could interact with the File objects even if I didn't use an explicit object reference. This was surprisingly easy once I knew about ObjectSpace.

# List all open File objects.
ObjectSpace.each_object(File) do |f|
  puts "%s: %d" % [f.path, f.fileno] unless f.closed?
end

# List the "dangling" File object which we didn't store in a variable.
ObjectSpace.each_object(File) do |f|
  unless f.closed?  
    printf "%s: %d\n", f.path, f.fileno unless f === filehandle
  end
end

# Close any dangling File objects. Ignore already-closed files, and leave
# the "accessible" object stored in *filehandle* alone.
ObjectSpace.each_object(File) {|f| f.close unless f === filehandle rescue nil}

Conclusion

There may be other ways to do this, but this is the answer I came up with to scratch my own itch. If you know a better way, please post another answer. The world will be a better place for it.

Palikar answered 6/6, 2012 at 20:48 Comment(3)
For anyone who cares about any open file descriptors that are not just actual files on disk, you can modify the code above to: ObjectSpace.each_object(IO) { |f| puts f unless f.closed? }Bobette
I wonder if a (Ruby) child process (i.e. after a fork) will see File objects for each of the (inherited) open file descriptors.Rancell
Very helpful, thanks. To elaborate on Liron's point, when using this approach, I think one should think about which subclasses of IO to include or exclude (e.g., do you want stdin, stdout, stderr?). Also, though it may be obvious to some, this expression will return all open IO's: ObjectSpace.each_object(IO).reject(&:closed?) This returned for me the following in Ruby 3.0.1's irb: [#<IO:<STDERR>>, #<IO:<STDOUT>>, #<IO:<STDIN>>, #<IO:fd 1>, #<IO:fd 0>].City

© 2022 - 2024 — McMap. All rights reserved.