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

Fellow monks: I'm still learning this, but here's my problem. Despite using a timeout, the "$response = $ua->request($request);" below often hangs. What am I doing wrong here? I've considered wrapping the get, timeout, and request, in some sort of a block and killing it if it times out, but there's got to be a better way.
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";
Thanks in advance, Beau

Replies are listed 'Best First'.
Re: HTTP Request Hangs
by pg (Canon) on Mar 03, 2003 at 18:53 UTC
    The problem resides in IO::Socket::INET, on some platform, when you use a big timeout (I know 60 sounds not big at all ;-), sometime you just can not wake it up. You have to keep it vigilant, so try to use smaller timeout, if you can. (This happens even with 5.8.0)
Re: HTTP Request Hangs
by IlyaM (Parson) on Mar 03, 2003 at 18:39 UTC
    Try to enable debug logging in LWP to see what happens (see LWP::Debug). What version of Perl are you using? If you use 5.005 then it comes with IO::Socket module which has a bug in timeout handling (IIRC LWP uses this module). You should upgrade to fixed version in this case.

    --
    Ilya Martynov, ilya@iponweb.net
    CTO IPonWEB (UK) Ltd
    Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
    Personal website - http://martynov.org

      Running Perl 5.6.0 on Sun. I'll add the LWP::Debug and see what that shows. Any suggestions on how to wrap the request in code that will kill it if it does not complete in a given time (like kicking a job off in BG and killing it it it takes too long)? Thanks for the LWP::Debug tip.