This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket module sometimes loses response packets
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.6, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: bkline
Priority: normal Keywords:

Created on 2017-02-27 17:11 by bkline, last changed 2022-04-11 14:58 by admin.

Messages (1)
msg288648 - (view) Author: Bob Kline (bkline) * Date: 2017-02-27 17:11
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.
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73853
2017-02-27 17:11:41bklinecreate