vancetech has asked for the wisdom of the Perl Monks concerning the following question:
I have a subroutine that is for timing out a traceroute attempt to a host. The routine is called like this, get_traceroute( 'microsoft.com' ).
For some hosts that often hang indefinately on the last hop to the host, I've implemented a timeout in the subroutine to stop the traceroute call after 10 seconds. However I am still seeing a LONG period of hanging for some hosts that is happening AFTER the timeout is reached, and when the subroutine is trying to CLOSE the pipe.
Why is this? By code is below
sub get_traceroute() { my $hostname = $_[0]; my $buf = ''; if( open my $pipe, "traceroute -I -n $hostname |" ) { my $start = [gettimeofday]; 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 tv_interval($start, [gettimeofday]) >= 2; } close $pipe; } return $buf; }
UPDATE:
Talking with tye he's pointed out that close() does a wait() which is still waiting for the traceroute child to end. The best way, since I don't need any further output from the traceroute, is to kill the process. Open() returns the process id, so I can easily kill the traceroute.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Timing out a traceroute
by McDarren (Abbot) on Sep 16, 2006 at 05:24 UTC | |
|
Re: Timing out a traceroute
by msk_0984 (Friar) on Sep 16, 2006 at 05:38 UTC | |
|
Re: Timing out a traceroute
by shmem (Chancellor) on Sep 16, 2006 at 07:35 UTC | |
by vancetech (Beadle) on Sep 18, 2006 at 04:49 UTC |