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.


In reply to Timing out a traceroute by vancetech

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.