New submission from Bob Kline:

The socket module does not always return response packets which are 
successfully delivered to the client host. We ran into this problem with an 
HTTP request for which socket.recv() raised an exception instead of returning 
the 301 redirection response which the web server sent back:

socket.error: [Errno 10054] An existing connection was forcibly closed by the 
remote host

We did some packet tracing, and saw that there were six packets exchanged for 
the connection:

client: [SYN]
server: [SYN,ACK]
client: [ACK]
client: GET /cam HTTP/1.1 ...\r\n\r\n
server: HTTP/1.0 301 Moved Permanently ...\r\n\r\n
server: [RST,ACK]

The failure appears to be timing dependent. If the 301 response is processed 
quickly enough (the usual case), the socket.recv() call returns it successfully 
to the caller. If sufficient delay is introduced, however, the 301 response is 
discarded, and the exception is raised. This can happen, for example, if the 
socket library has to take the time to handle setting up a requested timeout. 
On a slow enough machine, the following code will reproduce the problem:

import socket
s = socket.create_connection(("www.cancer.gov", 80), timeout=5)
s.sendall("GET /cam HTTP/1.1\r\nHost: www.cancer.gov\r\n\r\n")
print s.recv(8192)

You might have to stick a time.sleep(.5) call right in front of the s.recv() 
call if you can't find a slow enough machine to test on.

This appears to be a Windows-specific problem, as I can't reproduce it on a 
Linux or OS X client, no matter how much of a delay is introduced.

Platform: Windows (any version) with Python 2.7 or Python 3.6.

----------
components: Library (Lib)
messages: 288648
nosy: bkline
priority: normal
severity: normal
status: open
title: socket module sometimes loses response packets
type: behavior
versions: Python 2.7, Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29667>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to