Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

LWP::UserAgent request ignores timeout when internal server error encountered

by Anonymous Monk
on Aug 14, 2018 at 11:31 UTC ( [id://1220316]=perlquestion: print w/replies, xml ) Need Help??

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

This took just over 3 minutes (default timeout). Why didn't the timeout of 10 seconds kick in?

Note I cannot reproduce as the internal server error is fixed, but I want to avoid the lengthy timeout in future

Using perl 5.8.0, here is code snippet:

my $method = 'POST'; my $url = "https://etc"; # note: actual url not shown here my $query_string = ""; # note: actual query string not shown here my $ua = new LWP::UserAgent; $ua->timeout(10); $ua->agent('Mozilla/4.6'); $ua->cookie_jar(HTTP::Cookies->new); $request = new HTTP::Request($method,$url); $request->content_type('application/x-www-form-urlencoded'); $request->content($query_string); $response_content = $ua->request($request)->content; LWP::Debug::debug("Response:\n$response_content\n");

Debug messages are as follows:

LWP::UserAgent::new: () LWP::UserAgent::proxy: LWP::UserAgent::request: () LWP::UserAgent::send_request: POST <URL here> LWP::UserAgent::_need_proxy: Not proxied LWP::Protocol::http::request: () LWP::UserAgent::request: Simple response: Internal Server Error Response: 500 Connect failed: connect: Connection timed out; Connection timed ou +t

Replies are listed 'Best First'.
Re: LWP::UserAgent request ignores timeout when internal server error encountered
by thanos1983 (Parson) on Aug 14, 2018 at 12:06 UTC

    Hello Anonymous Monk,

    Have a look on this previously similar answered question Hard timeout for LWP::UserAgent or similar, as it contains several different approaches to your problem.

    Update: I just wrote a very small sample of code that it looks like timeout is working. I do not know why it is not working for you.

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent (); use Time::HiRes qw[ time ]; my $ua = LWP::UserAgent->new; $ua->timeout(1); my $uri = "http://192.168.1.31/noexist.html"; print time . "\n"; my $response = $ua->post($uri); print time . "\n"; if ($response->is_success) { print $response->decoded_content; # or whatever } else { die $response->status_line; } __END__ $ perl test.pl 1534257027.70495 1534257028.72877 500 Can't connect to 192.168.1.31:80 (Connection timed out) at test.pl + line 18.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!

      Further investigation shows that timeout works for http but not for https

      Does this throw any further light on it?

        Counter-example as SSCCE:

        use strict; use warnings; use LWP::UserAgent; use Test::More tests => 1; my $ua = LWP::UserAgent->new (); $ua->timeout (5); my $start = time; my $res = $ua->get ('https://www.sco.com:666/'); my $duration = time - $start; cmp_ok ($duration, '<', 10, 'Short timeout triggered');
Re: LWP::UserAgent request ignores timeout when internal server error encountered
by sectokia (Pilgrim) on Aug 14, 2018 at 12:41 UTC
    Are you really running perl 5.8? What version of libwww-perl are you running?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1220316]
Approved by Eily
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found