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

Fellow monks, I've been attempting to discover why a an HTTP request simply hangs. I have turned on LWP debugging via "use LWP::Debug qw(+)", and while this is proving highly informative, it is not pointing to anything. Sooooo, I am now seeking your wisdom regarding how I might wrap the get, timeout, and request, in some sort of a block so I can kill it if no response is received in nnn seconds (like killing off a hung background job). Code follows:
my $ua; my $url; my $request; my $response; my $timeout = 60; $ua = LWP::UserAgent->new(); $ua->agent("DMC-Monitor/1.0"); $request = new HTTP::Request("GET", "$url"); print "\$request=|$request|\n"; $response = $ua->timeout($timeout); print "\$response=|$response|\n"; $response = $ua->request($request); # <== Hangs here print "\$response=|$response|\n";
Your humble monk (OK, not so humble) bows deeply :-))

Replies are listed 'Best First'.
Re: HTTP Request Wrapper Wanted
by chromatic (Archbishop) on Mar 05, 2003 at 19:23 UTC

    If your platform supports it (if you're Unixy, you're probably okay), you can use alarm. I'm a little concerned that the timeout appears to be failing, but could be reading your code incorrectly.

      Thank you for the advise regarding alarm (I should've remembered that). Either I've implemented it incorrectly or the hanging HTTP request is also impacting the alarm because the request is still hanging and the alarm is NOT handling it as hoped. Here's the code with the alarm ..
      my $ua; my $url; my $msg; my $request; my $response; my $timeout = 50; $ua = LWP::UserAgent->new(); $ua->agent("DMC-Monitor/1.0"); $request = new HTTP::Request("GET", "$url"); print "\$request=|$request|\n"; $response = $ua->timeout($timeout); print "\$response=|$response|\n"; $msg = "Alarm Timeout!!\n"; eval { local $SIG{ALRM} = sub { die("$msg"); }; echo("alarm($timeout+2)\n"); alarm($timeout+2); # Timeout + 2 seconds. $response = $ua->request($request); # <== Hangs here alarm 0; print "\$response=|$response|\n"; }; print "$msg\n$@\n" if ($@);
      Thank you again,