Subject: | Open Filehandle Growth |
When using StandardHandles the coder must be careful to close the delegates themselves otherwise there will be infinite open handle growth.
Without the "close_fh" call on STDIN, this script will eventually build up tens of thousands of open file handles despite not touching stdin itself.
I had expected that when the AnyEvent::Subprocess object went out of scope that it would close the handles themselves. I am not actually touching STDIN myself, just capturing STDOUT so I was a surprised that the stdin delegate leaked.
Please see attached sample script. It leaks handles on both FreeBSD 9.1 and Ubuntu with Linux 3.8 kernels.
Subject: | fh.pl |
#!/usr/bin/perl
use v5.14.2;
use AnyEvent::Subprocess;
for my $i (0 .. 100_000) {
do_work();
}
sub do_work {
my $job = AnyEvent::Subprocess->new(
delegates => [ 'CompletionCondvar', 'StandardHandles' ],
code => sub {
exit 0;
}
);
my $run = $job->run;
my $condvar = $run->delegate('completion_condvar');
my $done = $condvar->recv;
# Close delegate handles
#
# Comment this out and the process will grow an infinite number of open
# file handles.
# $run->delegate('stdin')->handle->on_drain( sub { $_[0]->close_fh } );
return;
}