Perl: Using alarms with Post request produces odd

2019-07-25 21:06发布

Initial Question

I am trying use Perl to make a POST to a remote server. I am using an alarm to set a hard timeout. When the alarm triggers, I am getting what I would consider to be strange behaviour.

This is the code:

eval {  

    local $SIG{ALRM} = sub {
        die("Foobar");
    };
    alarm 25;


    my $userAgent = new LWP::UserAgent( keep_alive => 1 );
    $answer = $userAgent->post( ... );

    # During a timeout, I expect that this code will not run.  However,
    # it does and it prints "Foobar".
    $m = $answer->message();
    print $m;
    alarm 0;

};
alarm 0;        
print "Done";

Put a sleep (or breakpoint) on server, so it will not respond and so that the alarm will trigger. When the alarm triggers, this will be printed:

Foobar
Done

My expectation was that this should print:

Done

Key questions:

  • Why is this happening? Am I using some kind of anti-pattern? Is using alarms, not a good idea, because underlying libraries may use them as well, and they may conflict?

  • What is the right way to solve this problem?


Appendix 1 - I know there is another method...

I know that I should be using:

$userAgent->timeout( ... ); 

And actually, I am. However, I would like to set a hard timeout as well, so that I can ensure that at most I will spend 25 seconds waiting on the request. Since the timeout associated with $userAgent->timeout( ... ); is reset each time the client gets something back from the server, it is not reliable enough.


Appendix 2 - Environment Info

@bolav mentioned that on his system, he could not reproduce the issue, I guess that it is possible that it is System dependant.

OS:

cat /etc/redhat-release 
CentOS release 6.6 (Final)

Perl Version:

This is perl, v5.6.1 built for i686-linux

Appendix 3 - Answers on SO suggesting to use this method

0条回答
登录 后发表回答