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

_RttMonitor very rarely keeps its connection alive after MongoClient.close()

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.11
    • Affects Version/s: None
    • Component/s: None
    • None

      _RttMonitor very rarely keeps its connection alive after MongoClient.close() which leads to this rare test failure:

      [2020/07/02 02:31:25.893] FAIL [0.027s]: test_close_closes_sockets (test_client.TestClient)
      [2020/07/02 02:31:25.893] ----------------------------------------------------------------------
      [2020/07/02 02:31:25.893] Traceback (most recent call last):
      [2020/07/02 02:31:25.893]   File "/data/mci/8c8a2ace2ab093d7cf40fb6263df2b44/src/test/test_client.py", line 830, in test_close_closes_sockets
      [2020/07/02 02:31:25.893]     self.assertFalse(server._monitor._rtt_monitor._pool.sockets)
      [2020/07/02 02:31:25.893] AssertionError: deque([SocketInfo(<socket.socket fd=15, family=AddressFamily.AF_INET, type=526337, proto=6, laddr=('127.0.0.1', 51635), raddr=('127.0.0.1', 27017)>) at 140695887119808]) is not false
      

      https://evergreen.mongodb.com/task/mongo_python_driver_tests_python_version_rhel62_without_c_extensions__platform~rhel62_auth_ssl~noauth_nossl_python_version~3.4_c_extensions~without_c_extensions_coverage~coverage_test_4.4_replica_set_4457714d1b1a9f2e0d3d8b73fb913d024e7512dc_20_07_02_01_32_58

      The problem happens because _RttMonitor.close() can race with _RttMonitor._ping(). A solution would be to check for close() after checking out a connection:

          def _ping(self):
              """Run an "isMaster" command and return the RTT."""
              with self._pool.get_socket({}) as sock_info:
                  if self._closed:
                      raise Exception("closed")
      

      Note that I've marked this "minor" because this race is rare and the connection will still be closed when the client is garbage collected.

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: