No error while sending via TCP Socket

Grant Edwards grante at visi.com
Mon Jul 3 10:23:01 EDT 2006


On 2006-07-03, Ben Sizer <kylotan at gmail.com> wrote:

>>> and give me a hint how to get an exception
>>
>> You can't -- unless you've enabled the keepalive option on the
>> TCP connection and you've waited the requisite time after the
>> cable is cut before sending your data (IIRC it takes a couple
>> hours for an idle TCP connection to time out because of a link
>> being down).
>>
>> TCP/IP is designed to be fault-tolerant.  Temporary breaks in
>> cables aren't supposed to cause failures.
>
> But surely a permanent break - which is what I believe was
> implied - means the data never arrives, which in turn means
> you never get an ack, and eventually the TCP connection should
> drop,

Correct.  But in the case described by the OP, he disconnects
the cable when there is no traffic.  Unless the keepalive
feature has been enabled, TCP won't detect the cable is broken
until some time _after_ the OP has written data to it [it won't
timeout until after the stack send and resends that data
repeatedly and doesn't get an ACK].  

There's no way the TCP stack can raise an exception when the
data is written, because the TCP stack hasn't yet discovered
that the link is dead.

> and Python should raise an exception. Right?

No.  The TCP connection timeout only changes the state of the
socket.  There's no way for the TCP stack to cause an exception
in a Python program until the Python program accesses the
socket again after the timeout has occurred.

You'll get an error when you try to write to the closed
connection or an EOF when you try to read from the closed
connection.

> I'm very used to connections dropping after much less than a
> minute because the host became unreachable or took too long to
> send a response.

You're talking about the case where there's un-ACKed data.
Breaking a link when there's no un-ACKed data (which is what
the OP did) will require an hour or two to timeout _iff_
keepalive is enabled.  If keepalive has not been enabled, a
broken connection with no un-ACKed data will never timeout.

-- 
Grant Edwards                   grante             Yow!  .. I must be a
                                  at               VETERINARIAN...
                               visi.com            



More information about the Python-list mailing list