Re: creating a socket connection timeout in "C" linux



Alex Fraser <me@xxxxxxxxxxx> writes:
Rainer Weikusat wrote:
Alex Fraser <me@xxxxxxxxxxx> writes:
Rainer Weikusat wrote:
d-fan <rafel.coyle@xxxxxxxxxxxxxx> writes:
How can I open a socket connection with a timeout?
[...]

the easy, unreliable way to enforce a shorter timeout is alarm(3).
What makes alarm() unreliable?
I was being too imprecise. The 'easy, unreliable timeout' is setting
up a no-op handler for SIGALRM and relying on the alarm signal to
interrupt the connect. The problem is that 'setting up an alarm' and
'starting to connect' are two operations instead of one atomic
operation and their can be a near arbitrary delay between them because
other processes could be scheduled. This means there is a chance that
the SIGALRM may actually be posted to the process before the connect
ever blocks.

But you'd have the same "near arbitrary delay" even if there was an
atomic "connect() with timeout" call, between timeout/connect success
and the thread being scheduled again. So are you any worse off?

"Well, a piece of orbital junk could smash the building you are living
in at any time so it would clearly be useless to prevent it from
burning down by not making a large fire in the living room before
leaving for a three week holiday trip."

Relying on (a single) SIGALRM to interrupt a blocked system call could
lead to a lost wakeup in particular circumstances. This potential
problem can be avoided by suitable code, ie by longjumping from the
handler instead. This still doesn't mean that connect was ever
actually called, but that the program has done every possible thing to
regain control after the timeout expired. Which in no way implies that
some other problem could not cause a similar effect.


.



Relevant Pages

  • Re: perl ssh with timeout?
    ... Use alarm to send yourself a SIGALRM in $timeout seconds, ... perldoc -q keyword ...
    (comp.lang.perl.misc)
  • My versin of sleep... comments?
    ... That document lists lots of ways in which SIGALRM and sleep ... unsigned prev_alarm, slept, unslept, effective_secs; ... We save the old signal mask in oset. ... We turn off the alarm, in case it's set, and save the remaining time. ...
    (comp.unix.programmer)
  • Re: Time out question
    ... There are quite a lot of problems with alarm() ... from time_limit import time_limit, TimeOut ... raise TimeOut() ... # Install new handler remembering old ...
    (comp.lang.python)
  • Re: Bash: command output to variable
    ... ReadMode "normal"; ... # reads key with a timeout of 5 seconds without echo ... alarm $timeout; ... # if the Perl syntax error message string exists and is not ...
    (comp.unix.shell)
  • Re: My versin of sleep... comments?
    ... That document lists lots of ways in which SIGALRM and sleep ... You're aware that many implementations of sleep() avoid alrm ... We save the old signal mask in oset. ... We turn off the alarm, in case it's set, and save the remaining time. ...
    (comp.unix.programmer)