Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to test for open file handles?

by jdrago_999 (Hermit)
on Feb 03, 2009 at 23:32 UTC ( #741142=perlquestion: print w/ replies, xml ) Need Help??
jdrago_999 has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I was recently bit by the "Too many open files" error while doing a lot of file operations via a Perl script.

After (several hundred or thousand) file handles had been opened (and not closed), my program would just crash.

Sure - now I've fixed the problem, but it would be nice to add a unit test to check for dangling open file handles. A quick search on CPAN didn't produce anything, and without resorting to tricks like $open_filehandles = `/usr/sbin/lsof -p $$ | wc -l` I'd like to know how to do this in Perl.

Ideas?

Comment on How to test for open file handles?
Download Code
Re: How to test for open file handles?
by roboticus (Canon) on Feb 04, 2009 at 00:36 UTC
    jdrago 999:

    Perhaps it would be better to "mock" the filehandle by commandeering the open & close methods. Then open could increment a variable, and close could decrement it. At the end of your test, the variable should be zero. I'm sure there's a way to intercept the core functions, but I don't know how to do it, so I can't offer any assistance on the actual implementation.

    ...roboticus
      Perhaps it would be better to "mock" the filehandle by commandeering the open & close methods.

      I'm currently using IO::File. I could subclass it, do some singleton whiz-bang stuff and keep track of opens vs closes there.

      I just figured there might be a special Perl variable that would tell me how many files had been opened - something I could check for changes over time.

Re: How to test for open file handles?
by Joost (Canon) on Feb 04, 2009 at 01:28 UTC
      FileCache - keep more files open than the system permits
Re: How to test for open file handles?
by kyle (Abbot) on Feb 04, 2009 at 05:29 UTC

    The way I've tested this in the past is to loop over the suspect code a few thousand times. If it does not die, it's not leaking open files.

    Update: Fixed a typo. Thanks GrandFather!

Re: How to test for open file handles?
by repellent (Priest) on Feb 04, 2009 at 05:31 UTC
    As a preventive measure, I'd suggest restructuring code to make use of lexical filehandles as shown. Always open a lexical $FH in a BLOCK:
    { open(my $FH, "<", $filename) or die("Cannot open: $filename\n $!"); while (<$FH>) { ... }; # unnecessary - $FH gets closed once it leaves lexical scope #close($FH); }
      I'd argue that the close is necessary. Sure the file handle will be closed implicitly when it goes out of scope but then you can't check the result. It's just as import to check the return value of close() as it is to check the return value of open, if not more so... particularly when a file is being written to. That, said lexically scoped file-handles are always a good idea.
Re: How to test for open file handles?
by NateTut (Deacon) on Feb 04, 2009 at 15:25 UTC
    A tip: I always code the close at the same time as the open to make sure I close files as soon as I'm done with them. This not only reduces the chance of the "too many open files" type of errors it also reduces the time the file is locked and reduces the chance of file corruption if your program crashes with open files.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://741142]
Approved by sweetblood
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (15)
As of 2014-08-22 14:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (158 votes), past polls