Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
eval { local $SIG{ALRM} = sub { die "timeout" }; alarm($timeout); connect(SOCKET, $paddr) || error(); alarm(0); };

What is happening is (most likely) this:  when there's nothing listening on the port in question, the connect() fails more or less immediately ("connection refused") and the (non-existent) routine error() is being called. This dies with "Undefined subroutine &main::error called at...", so the alarm(0) isn't getting executed.  As the latter is supposed to reset the timer set up with the initial alarm($timeout), the timer keeps running and then fires later, outside of the eval{} scope, where the localized $SIG{ALRM} handler is no longer in effect...

In other words, the uncaught ALRM exception (on Windows emulated via SetTimer()/Windows Messages) is being reported as "Terminating on signal SIGALRM(14)".

Simplified demo:

#!perl eval { local $SIG{ALRM} = sub { die "timeout" }; alarm(1); error(); # not found -> dies alarm(0); }; print "\$@: $@\n" if $@; # do something that takes longer than a sec, # without itself being implemented via SetTimer() rand for 1..2e7; __END__ H:\PM>perl 759131.pl $@: Undefined subroutine &main::error called at 759131.pl line 6. Terminating on signal SIGALRM(14)

Solution:  either move the alarm(0) outside of the eval{} (so it's always being executed), or set $SIG{ALRM} ='IGNORE' outside of the eval{} to ignore the timer 'signal'.


In reply to Re: Detecting a port "in use" on localhost:$port by almut
in thread Detecting a port "in use" on localhost:$port by freddo411

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found