Closing all open file descriptors

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

===============================================================================

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

"Ara.T.Howard" <Ara.T.Howard@noaa.gov> schrieb im Newsbeitrag
news:Pine.LNX.4.60.0409141123490.29014@harp.ngdc.noaa.gov...

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

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

Cheers,
Kent.

···

On Sep 14, 2004, at 1:44 PM, Ara.T.Howard wrote:

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