Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-356

Windows localhost connection slowdowns.

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.1
    • 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.

            Assignee:
            bernie@mongodb.com Bernie Hackett
            Reporter:
            bernie@mongodb.com Bernie Hackett
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: