closing all open file descriptors

Discussion in 'Ruby' started by Ara.T.Howard, Sep 14, 2004.

  1. Ara.T.Howard

    Ara.T.Howard Guest

    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
    ===============================================================================
     
    Ara.T.Howard, Sep 14, 2004
    #1
    1. Advertising

  2. Ara.T.Howard

    Aredridel Guest

    > 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
     
    Aredridel, Sep 15, 2004
    #2
    1. Advertising

  3. "Ara.T.Howard" <> schrieb im Newsbeitrag
    news:p...
    >
    > 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
     
    Robert Klemme, Sep 15, 2004
    #3
  4. Ara.T.Howard

    Kent Sibilev Guest

    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
    > =======================================================================
    > ========
    >
     
    Kent Sibilev, Sep 15, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. puzzlecracker
    Replies:
    8
    Views:
    1,277
    Chris Uppal
    Sep 7, 2006
  2. JG
    Replies:
    5
    Views:
    430
    Lawrence Kirby
    Feb 8, 2005
  3. Yang
    Replies:
    3
    Views:
    868
  4. MenTaLguY

    closing file descriptors

    MenTaLguY, Apr 24, 2007, in forum: Ruby
    Replies:
    1
    Views:
    90
    Gary Wright
    Apr 24, 2007
  5. Rex Gustavus Adolphus

    how to see how many open file descriptors my program has?

    Rex Gustavus Adolphus, Mar 4, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    149
    pkent
    Mar 6, 2004
Loading...

Share This Page