Minimizing Connection reset by peer exceptions

M

mirandacascade

This may be more of a socket question than a python question; not sure.

Using this code to instantiate/connect/set options
connectionHandle = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
errorStatus = connectionHandle.connect_ex((ipAddress, port))
connectionHandle.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO,
60000)

Using this code to send:
retSendAll = connectionHandle.sendall(messageToHost)

Followed by this code to recv:
bufferSize = 500000
responseBuffer = connectionHandle.recv(bufferSize)

Occasionally (perhaps 5% of the time) the following exception gets
raised:

(10054, 'Connection reset by peer')


Are there any changes I can make to the code above to eliminate the
10054 errors or to reduce the probability of encountering the 10054
error? Are there any settings that make the 'Connection reset by
peer' condition less likely?

Other posts on this subject seem to suggest that this can only be
handled by:
1) detecting the 10054 error
2) issuing a message explaining the 'connection reset' condition
followed by something along the lines of 'try again later'.
 
B

Ben Sizer

Occasionally (perhaps 5% of the time) the following exception gets
raised:

(10054, 'Connection reset by peer')

Generally this just means the connection has closed through some
unusual means, perhaps by being turned off, or a network cable being
unplugged, or a timeout along the way, etc. 5% is a high figure, but
perhaps you connect to hosts that are unreliable for some reason.

You don't have control over this really; just make sure you handle the
exception. Such is life, when dealing with networking.
 
S

Steve Holden

Ben said:
Generally this just means the connection has closed through some
unusual means, perhaps by being turned off, or a network cable being
unplugged, or a timeout along the way, etc. 5% is a high figure, but
perhaps you connect to hosts that are unreliable for some reason.

You don't have control over this really; just make sure you handle the
exception. Such is life, when dealing with networking.
Do note, though, that if you aren't using some means (threading,
forking, etc) of handling the connections asynchronously then your
server will normally only queue a very limited number of connections
(usually 5 at most).

So if your service takes a while to run then it's possible that
connection requests will be rejected when the queue is full, which might
*possibly* result in the error you are seeing.

Feel free to ignore this if you only have one client at a time.

regards
Steve
 
B

Ben Sizer

Steve said:
Do note, though, that if you aren't using some means (threading,
forking, etc) of handling the connections asynchronously then your
server will normally only queue a very limited number of connections
(usually 5 at most).

The example given by the original poster seemed to be a client (using
connect_ex) rather than a server, so I think this would only be an
issue if the code was connecting to the same host repeatedly in quick
succession.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top