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

Hi Monks!!

I would like implement a timeout on lwp using the https protocol, i know that's have a bug and not work at least on win, so i think in that:

#!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new( GET => "https://www.amazon.com" ); my $page = ""; eval {local $SIG{ALRM}=sub{die("timeout\n");}; alarm 40; my $res = $ua->request($req); if ($res->is_success) { $page = $res->content; } alarm 0; };

But not works too, What's the correct workaround to solve the timeout problem on https protocol?

Thk U

Replies are listed 'Best First'.
Re: Workaround of Timeout on https
by starbolin (Hermit) on May 14, 2008 at 16:13 UTC

    First, why aren't you using the timeout() member of LWP::UserAgent? Second, your code does not capture the return from die(). Remember, die() exits the eval() setting $@ with the string given. You need to check the status of $@ outside your eval().

    Updtate: see also LWP - timeout


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

      Hi, thk, in fact i checking the status of $@ but not works too, i'm not use timeout here becouse doesn't work see the report http://rt.cpan.org/Public/Bug/Display.html?id=22839, i see your post but it's from 2001 actually win32 support signal.. i guess... becouse I was used alarm from 5 year with problem....

      Thk U very much really

        Not all processes in the network stack are interruptible. Perl can't process an ALRM signal until control is returned to the perl interpreter. One partial remediation is to break up the task into small chunks. ie. Perform DNS queries first and pinging the server before issuing GETs.

        The only sure 'workaround' is to fork a child to handle the network communication. This allows the parent to time-out and leave the children to the reaper.


        s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}