-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 2.2
-
Component/s: None
-
None
A PyMongo 2.2 change to solve some problems with how we handle IPv6 connections uncovered an issue with TCP connections under Windows. When a Windows client makes a socket connection to an unused port the Winsock stack will retry the failed connection 3 times, causing the connection failure to take over a second. This misfeature is documented here:
http://support.microsoft.com/kb/175523
Although this problem is not specific to Python, see this thread for further discussion:
http://mail.python.org/pipermail/python-dev/2009-January/085054.html
The problem can be seen using the following code (note nothing is listening on port 27018):
>>> import socket >>> import timeit >>> s = socket.socket() >>> def conn(): ... try: ... s.connect(('localhost', 27018)) ... except: ... pass ... >>>
Timing the 'conn' function on Windows takes over a second:
>>> timeit.timeit(conn, number=1) 1.0130121439588038
On Linux (or MacOS) the connection failure happens immediately:
>>> timeit.timeit(conn, number=1) 0.0002570152282714844
A workaround for connecting on localhost is to pass '127.0.0.1' instead of 'localhost' or nothing for hostname:
conn = pymongo.Connection('127.0.0.1')
For PyMongo 2.2.1 we will change the order of protocol attempts to always try IPv4 first (the previous behavior). In PyMongo 2.3 we will likely add an 'ipv6' option to the Connection class to further reduce the problem.
This Winsock behavior likely means Windows users should be using a lower connectTimeoutMS setting since no change that can be made in PyMongo can completely work around the underlying issue.
- is related to
-
TOOLS-2301 Dramatic connection time difference for mongodump/mongorestore on Windows since 3.4
- Accepted
-
GODRIVER-1196 Slow localhost connection on Windows
- Closed
-
PYTHON-2280 Investigate slow OCSP endpoint checks on Windows
- Closed
-
PYTHON-2209 Support resolving "localhost" to IPv6
- Closed
- related to
-
CDRIVER-3639 Connection failure may take 1-2s when connecting to the wrong address on Windows
- Backlog