closing all open file descriptors

A

Ara.T.Howard

i just discovered a subtle bugette (no harm done except nfs silly name
created) in some code i had written that is caused by forking with open file
descriptors followed by a close/unlink in the parent. the child never uses
the file and so it's not really an 'error' - but on nfs this will cause a
sillyname to appear (.nfsxxxxxxxx) and remain until the child exits which, in
this case, can be up to five days later and i have dozens of these guys
cluttering up my directories since this code runs on dozens of machine
simoutaneously.

i'm wondering - is there some way in ruby to obtain the list of all open file
desriptors so that something akin to fdwalk might be used to close all open
descriptors > fileno(stderr).

all i can think now is

3.upto(65536){|fd| IO::new(fd).close rescue nil}

which is incredibly expensive!

you could set the close on exec flag of all of them too - but getting the list
of all open fds is just as hard... ;-(

i've thought of tracking calls to open by supplanting ruby's File#open, but i
am using every extension which do their own opening too so this is out...

ideas?

cheers.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| A flower falls, even though we love it;
| and a weed grows, even though we do not love it.
| --Dogen
===============================================================================
 
A

Aredridel

all i can think now is
3.upto(65536){|fd| IO::new(fd).close rescue nil}

which is incredibly expensive!

That's how it's usually done in C, though often only to 256.

If it's inefficient, a File.close_all(from) method might be nice as a
C extension.

Ari
 
R

Robert Klemme

Ara.T.Howard said:
i just discovered a subtle bugette (no harm done except nfs silly name
created) in some code i had written that is caused by forking with open file
descriptors followed by a close/unlink in the parent. the child never uses
the file and so it's not really an 'error' - but on nfs this will cause a
sillyname to appear (.nfsxxxxxxxx) and remain until the child exits which, in
this case, can be up to five days later and i have dozens of these guys
cluttering up my directories since this code runs on dozens of machine
simoutaneously.

i'm wondering - is there some way in ruby to obtain the list of all open file
desriptors so that something akin to fdwalk might be used to close all open
descriptors > fileno(stderr).

all i can think now is

3.upto(65536){|fd| IO::new(fd).close rescue nil}

which is incredibly expensive!

you could set the close on exec flag of all of them too - but getting the list
of all open fds is just as hard... ;-(

Is it?
ObjectSpace.each_object(IO) {|io| p [io, io.class, io.closed?]}
[#<File:/usr/lib/ruby/1.8/irb.rb (closed)>, File, true]
[#<File:/usr/bin/irb (closed)>, File, true]
[#<IO:0x100ee000>, IO, false]
[#<IO:0x100ee018>, IO, false]
[#<IO:0x100ee030>, IO, false]
[#<File:/cygdrive/c/DOKUME~1/ROBERT~1.KLE/LOKALE~1/Temp/_usr_lib_ruby_1_8_
irb_lc_error_rb1464.0 (closed)>, File, true]
[#<File:/usr/lib/ruby/1.8/irb/locale.rb (closed)>, File, true]
[#<File:amakecr.bat>, File, false]
[#<File:/cygdrive/c/DOKUME~1/ROBERT~1.KLE/LOKALE~1/Temp/_usr_lib_ruby_1_8_
irb_lc_error_rb1464.0 (closed)>, File, true]
=> 9

Kind regards

robert
 
K

Kent Sibilev

On Linux, you can obtain the list of file descriptors by looking into
the /proc/self/fd directory.

Cheers,
Kent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top