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

I've been encountering a problem where whenever I run some code which makes an LWP request any alarms I set aren't being captured. The process just carries on until something else causes it to die.

I played around a bit, trying to find out what was going on. And came up with the code below for testing the behaviour.

I set an alarm, make a request and then sleep for 10 more seconds than the alarm period.

This gave me the following
Got page
0 seconds left
No Problems
23 wallclock secs ( 0.13 usr + 0.01 sys = 0.14 CPU)

It appears that something to do with LWP has cancelled my alarm.

Any suggestions on fixes/workarounds for this?
My perl versions are:

LWP 5.50
LWP::Authen::Basic undef
LWP::Authen::Digest undef
LWP::Debug undef
LWP::MediaTypes 1.27
LWP::MemberMixin undef
LWP::Protocol 1.36
LWP::Protocol::GHTTP undef
LWP::Protocol::data undef
LWP::Protocol::file undef
LWP::Protocol::ftp undef
LWP::Protocol::gopher undef
LWP::Protocol::http undef
LWP::Protocol::https undef
LWP::Protocol::mailto undef
LWP::Protocol::nntp undef
LWP::RobotUA 1.17
LWP::Simple 1.33
LWP::UserAgent 1.74

IO undef
IO::File 1.06021
IO::Handle 1.1505
IO::Pipe 1.0901
IO::Seekable 1.06
IO::Select 1.10
IO::Socket 1.1603

Thanks in advance for any help.
#!/usr/bin/perl use strict; # set up modules for LWP use LWP::UserAgent; use Benchmark; my $start = new Benchmark; my $url = 'http://www.bbc.co.uk'; #set alarm and sleep times my $alarm = 10; my $sleep = $alarm + 10; #set alarm sig local $@; local $SIG{ALRM} = sub {die "TIMEOUT" }; #start the alarm alarm $alarm; #wrap it in an eval eval{ #do the lwp stuff lwp_post($url); #sleep sleep $sleep; }; #reset alarm my $remaining = alarm 0; print "$remaining seconds left\n"; if ($@) {print "Error Occured [$@]\n";} else {print "No Problems\n";} my $end = new Benchmark; my $elapsed = timediff($end,$start); print timestr($elapsed); sub lwp_post{ my $address = shift; my $headers = new HTTP::Headers(Accept => 'text/plain', 'User-Agent' => 'LWP Test/1.0'); my $url = new URI::URL($address); my $request = new HTTP::Request('GET', $url, $headers); my $ua = new LWP::UserAgent; my $response = $ua->request($request); if ($response->is_success) { print "Got page\n"; } else { print "Failed to get page\n"; } }

Replies are listed 'Best First'.
Re: Alarms broken when using LWP
by tomhukins (Curate) on Mar 29, 2001 at 20:36 UTC

    See the documentation for LWP::UserAgent.

    By default the library uses alarm() to implement timeouts, dying if the timeout occurs.

    Your alarms are interfering with LWP's. From your code, it seems the purpose of your alarm is to force a timeout for your connection. You should use LWP::UserAgent's timeout method for this.

    You could search on LWP timeout for more information.

      That's an old version of LWP. Modern versions (in the past two years, if I recall correctly) use select timeouts, not alarm timeouts.

      Solution: upgrade to something released in the past two years!

      -- Randal L. Schwartz, Perl hacker