vancetech has asked for the wisdom of the Perl Monks concerning the following question:

The message "kern.ipc.maxpipekva exceeded" is showing up on a development box ( FreeBSD 5.4 ) where I'm running a perl daemon that is doing a number of traceroutes to various hosts over time. From what I can tell, this leak is in the method I'm performing the traceroutes.

The longer the process runs, the more pipes that seem to remain open on the system, eventually reaching maxpipekva.

I can see in sysctl that kern.ipc.pipes and kern.ipc.pipekva bob up and down slowing approaching the maximum.

I can also see, using fstat, that there are a growing number of pipes connected to my daemon. The shell process spawned to deal with the traceroute has a number of pipes associated with them but disappear when the traceroute finally times out.

I'm using the following code to gather the traceroute:
eval { local $SIG{ALRM} = sub { die "timeout" }; alarm( 10 ); $temp = `traceroute -I -n $hostname > $data_file_dir/traceroute.$$ +`; alarm( 0 ); };
I assume that the traceroute in backticks will be terminated after the ALRM signal is sent, but I suspect that this is not the case and the traceroute program is waiting to perform the default of all 64 hops before terminating.

How can I avoid reaching the maxpipekva limit?

Replies are listed 'Best First'.
Re: Leaky pipes?
by salva (Canon) on Mar 22, 2006 at 09:17 UTC
    well, using signals from perl is not always a safe operation.

    The problem in your case is probably that at the low level, inside the perl interpreter, the die statement long-jumps over the cleanup code of the backtick operator where the pipes used to communicate with the slave process are closed.

    There isn't an easy workaround. Maybe some module on CPAN would do it... though you should check that it doesn't use alarm internally.

    A safe (untested) way to do it in perl is:

    open my $pipe, "traceroute ... |" or die; my $start = time; my $buf = ''; while(1) { my $vin = ''; vec($vin, fileno($pipe), 1) = 1; if (select($vin, undef, undef, 1)) { sysread $pipe, $buf, 2048, length $buf or last; } last if time - $start > $timeout; } close $pipe; # traceroute output is in $buf now
      Thankyou for your concept and code salva, that looks a lot more polite than an alarm signal.