I just tracked down a problem where I had to close all open filehandles for my Apache cgi script to continue. I traced the problem to Parse::RecDescent.
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
$|++;
print "Content-Type: text/plain\n\n";
use Parse::RecDescent;
say "$$: pre-fork: ". time;
if(my $pid = fork) {
# parent
say "$$: return immediately: ". time;
}
else {
# child
say "$$: kicked off big process: ". time;
close STDIN;
close STDOUT;
close STDERR;
# close *{'Parse::RecDescent::ERROR'};
sleep 5;
}
My question is how do I find all open package filehandles?
I know fileno
will return a counter for an open filehandle.
Is there a way to do a reverse lookup for these, or close filehandles by their fileno
counter?
On some systems, the directory returned by
"/proc/$$/fd/"
contains the list of open file descriptors. You could usePOSIX::close
to close them.